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 - https://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 USB_TEST_J: 50 case USB_TEST_K: 51 case USB_TEST_SE0_NAK: 52 case USB_TEST_PACKET: 53 case USB_TEST_FORCE_ENABLE: 54 reg |= mode << 1; 55 break; 56 default: 57 return -EINVAL; 58 } 59 60 dwc3_gadget_dctl_write_safe(dwc, 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 (!DWC3_VER_IS_PRIOR(DWC3, 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 no action before sending new link state change */ 115 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 116 117 /* set requested state */ 118 reg |= DWC3_DCTL_ULSTCHNGREQ(state); 119 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 120 121 /* 122 * The following code is racy when called from dwc3_gadget_wakeup, 123 * and is not needed, at least on newer versions 124 */ 125 if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) 126 return 0; 127 128 /* wait for a change in DSTS */ 129 retries = 10000; 130 while (--retries) { 131 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 132 133 if (DWC3_DSTS_USBLNKST(reg) == state) 134 return 0; 135 136 udelay(5); 137 } 138 139 return -ETIMEDOUT; 140 } 141 142 static void dwc3_ep0_reset_state(struct dwc3 *dwc) 143 { 144 unsigned int dir; 145 146 if (dwc->ep0state != EP0_SETUP_PHASE) { 147 dir = !!dwc->ep0_expect_in; 148 if (dwc->ep0state == EP0_DATA_PHASE) 149 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]); 150 else 151 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]); 152 153 dwc->eps[0]->trb_enqueue = 0; 154 dwc->eps[1]->trb_enqueue = 0; 155 156 dwc3_ep0_stall_and_restart(dwc); 157 } 158 } 159 160 /** 161 * dwc3_ep_inc_trb - increment a trb index. 162 * @index: Pointer to the TRB index to increment. 163 * 164 * The index should never point to the link TRB. After incrementing, 165 * if it is point to the link TRB, wrap around to the beginning. The 166 * link TRB is always at the last TRB entry. 167 */ 168 static void dwc3_ep_inc_trb(u8 *index) 169 { 170 (*index)++; 171 if (*index == (DWC3_TRB_NUM - 1)) 172 *index = 0; 173 } 174 175 /** 176 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer 177 * @dep: The endpoint whose enqueue pointer we're incrementing 178 */ 179 static void dwc3_ep_inc_enq(struct dwc3_ep *dep) 180 { 181 dwc3_ep_inc_trb(&dep->trb_enqueue); 182 } 183 184 /** 185 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer 186 * @dep: The endpoint whose enqueue pointer we're incrementing 187 */ 188 static void dwc3_ep_inc_deq(struct dwc3_ep *dep) 189 { 190 dwc3_ep_inc_trb(&dep->trb_dequeue); 191 } 192 193 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep, 194 struct dwc3_request *req, int status) 195 { 196 struct dwc3 *dwc = dep->dwc; 197 198 list_del(&req->list); 199 req->remaining = 0; 200 req->num_trbs = 0; 201 202 if (req->request.status == -EINPROGRESS) 203 req->request.status = status; 204 205 if (req->trb) 206 usb_gadget_unmap_request_by_dev(dwc->sysdev, 207 &req->request, req->direction); 208 209 req->trb = NULL; 210 trace_dwc3_gadget_giveback(req); 211 212 if (dep->number > 1) 213 pm_runtime_put(dwc->dev); 214 } 215 216 /** 217 * dwc3_gadget_giveback - call struct usb_request's ->complete callback 218 * @dep: The endpoint to whom the request belongs to 219 * @req: The request we're giving back 220 * @status: completion code for the request 221 * 222 * Must be called with controller's lock held and interrupts disabled. This 223 * function will unmap @req and call its ->complete() callback to notify upper 224 * layers that it has completed. 225 */ 226 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, 227 int status) 228 { 229 struct dwc3 *dwc = dep->dwc; 230 231 dwc3_gadget_del_and_unmap_request(dep, req, status); 232 req->status = DWC3_REQUEST_STATUS_COMPLETED; 233 234 spin_unlock(&dwc->lock); 235 usb_gadget_giveback_request(&dep->endpoint, &req->request); 236 spin_lock(&dwc->lock); 237 } 238 239 /** 240 * dwc3_send_gadget_generic_command - issue a generic command for the controller 241 * @dwc: pointer to the controller context 242 * @cmd: the command to be issued 243 * @param: command parameter 244 * 245 * Caller should take care of locking. Issue @cmd with a given @param to @dwc 246 * and wait for its completion. 247 */ 248 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd, 249 u32 param) 250 { 251 u32 timeout = 500; 252 int status = 0; 253 int ret = 0; 254 u32 reg; 255 256 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); 257 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); 258 259 do { 260 reg = dwc3_readl(dwc->regs, DWC3_DGCMD); 261 if (!(reg & DWC3_DGCMD_CMDACT)) { 262 status = DWC3_DGCMD_STATUS(reg); 263 if (status) 264 ret = -EINVAL; 265 break; 266 } 267 } while (--timeout); 268 269 if (!timeout) { 270 ret = -ETIMEDOUT; 271 status = -ETIMEDOUT; 272 } 273 274 trace_dwc3_gadget_generic_cmd(cmd, param, status); 275 276 return ret; 277 } 278 279 /** 280 * dwc3_send_gadget_ep_cmd - issue an endpoint command 281 * @dep: the endpoint to which the command is going to be issued 282 * @cmd: the command to be issued 283 * @params: parameters to the command 284 * 285 * Caller should handle locking. This function will issue @cmd with given 286 * @params to @dep and wait for its completion. 287 * 288 * According to the programming guide, if the link state is in L1/L2/U3, 289 * then sending the Start Transfer command may not complete. The 290 * programming guide suggested to bring the link state back to ON/U0 by 291 * performing remote wakeup prior to sending the command. However, don't 292 * initiate remote wakeup when the user/function does not send wakeup 293 * request via wakeup ops. Send the command when it's allowed. 294 * 295 * Notes: 296 * For L1 link state, issuing a command requires the clearing of 297 * GUSB2PHYCFG.SUSPENDUSB2, which turns on the signal required to complete 298 * the given command (usually within 50us). This should happen within the 299 * command timeout set by driver. No additional step is needed. 300 * 301 * For L2 or U3 link state, the gadget is in USB suspend. Care should be 302 * taken when sending Start Transfer command to ensure that it's done after 303 * USB resume. 304 */ 305 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd, 306 struct dwc3_gadget_ep_cmd_params *params) 307 { 308 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 309 struct dwc3 *dwc = dep->dwc; 310 u32 timeout = 5000; 311 u32 saved_config = 0; 312 u32 reg; 313 314 int cmd_status = 0; 315 int ret = -EINVAL; 316 317 /* 318 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or 319 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an 320 * endpoint command. 321 * 322 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY 323 * settings. Restore them after the command is completed. 324 * 325 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2 326 */ 327 if (dwc->gadget->speed <= USB_SPEED_HIGH || 328 DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) { 329 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 330 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { 331 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; 332 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 333 } 334 335 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { 336 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; 337 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; 338 } 339 340 if (saved_config) 341 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 342 } 343 344 /* 345 * For some commands such as Update Transfer command, DEPCMDPARn 346 * registers are reserved. Since the driver often sends Update Transfer 347 * command, don't write to DEPCMDPARn to avoid register write delays and 348 * improve performance. 349 */ 350 if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_UPDATETRANSFER) { 351 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); 352 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); 353 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); 354 } 355 356 /* 357 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're 358 * not relying on XferNotReady, we can make use of a special "No 359 * Response Update Transfer" command where we should clear both CmdAct 360 * and CmdIOC bits. 361 * 362 * With this, we don't need to wait for command completion and can 363 * straight away issue further commands to the endpoint. 364 * 365 * NOTICE: We're making an assumption that control endpoints will never 366 * make use of Update Transfer command. This is a safe assumption 367 * because we can never have more than one request at a time with 368 * Control Endpoints. If anybody changes that assumption, this chunk 369 * needs to be updated accordingly. 370 */ 371 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER && 372 !usb_endpoint_xfer_isoc(desc)) 373 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT); 374 else 375 cmd |= DWC3_DEPCMD_CMDACT; 376 377 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); 378 379 if (!(cmd & DWC3_DEPCMD_CMDACT) || 380 (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER && 381 !(cmd & DWC3_DEPCMD_CMDIOC))) { 382 ret = 0; 383 goto skip_status; 384 } 385 386 do { 387 reg = dwc3_readl(dep->regs, DWC3_DEPCMD); 388 if (!(reg & DWC3_DEPCMD_CMDACT)) { 389 cmd_status = DWC3_DEPCMD_STATUS(reg); 390 391 switch (cmd_status) { 392 case 0: 393 ret = 0; 394 break; 395 case DEPEVT_TRANSFER_NO_RESOURCE: 396 dev_WARN(dwc->dev, "No resource for %s\n", 397 dep->name); 398 ret = -EINVAL; 399 break; 400 case DEPEVT_TRANSFER_BUS_EXPIRY: 401 /* 402 * SW issues START TRANSFER command to 403 * isochronous ep with future frame interval. If 404 * future interval time has already passed when 405 * core receives the command, it will respond 406 * with an error status of 'Bus Expiry'. 407 * 408 * Instead of always returning -EINVAL, let's 409 * give a hint to the gadget driver that this is 410 * the case by returning -EAGAIN. 411 */ 412 ret = -EAGAIN; 413 break; 414 default: 415 dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); 416 } 417 418 break; 419 } 420 } while (--timeout); 421 422 if (timeout == 0) { 423 ret = -ETIMEDOUT; 424 cmd_status = -ETIMEDOUT; 425 } 426 427 skip_status: 428 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); 429 430 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { 431 if (ret == 0) 432 dep->flags |= DWC3_EP_TRANSFER_STARTED; 433 434 if (ret != -ETIMEDOUT) 435 dwc3_gadget_ep_get_transfer_index(dep); 436 } 437 438 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER && 439 !(cmd & DWC3_DEPCMD_CMDIOC)) 440 mdelay(1); 441 442 if (saved_config) { 443 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 444 reg |= saved_config; 445 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 446 } 447 448 return ret; 449 } 450 451 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) 452 { 453 struct dwc3 *dwc = dep->dwc; 454 struct dwc3_gadget_ep_cmd_params params; 455 u32 cmd = DWC3_DEPCMD_CLEARSTALL; 456 457 /* 458 * As of core revision 2.60a the recommended programming model 459 * is to set the ClearPendIN bit when issuing a Clear Stall EP 460 * command for IN endpoints. This is to prevent an issue where 461 * some (non-compliant) hosts may not send ACK TPs for pending 462 * IN transfers due to a mishandled error condition. Synopsys 463 * STAR 9000614252. 464 */ 465 if (dep->direction && 466 !DWC3_VER_IS_PRIOR(DWC3, 260A) && 467 (dwc->gadget->speed >= USB_SPEED_SUPER)) 468 cmd |= DWC3_DEPCMD_CLEARPENDIN; 469 470 memset(¶ms, 0, sizeof(params)); 471 472 return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 473 } 474 475 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, 476 struct dwc3_trb *trb) 477 { 478 u32 offset = (char *) trb - (char *) dep->trb_pool; 479 480 return dep->trb_pool_dma + offset; 481 } 482 483 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) 484 { 485 struct dwc3 *dwc = dep->dwc; 486 487 if (dep->trb_pool) 488 return 0; 489 490 dep->trb_pool = dma_alloc_coherent(dwc->sysdev, 491 sizeof(struct dwc3_trb) * DWC3_TRB_NUM, 492 &dep->trb_pool_dma, GFP_KERNEL); 493 if (!dep->trb_pool) { 494 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", 495 dep->name); 496 return -ENOMEM; 497 } 498 499 return 0; 500 } 501 502 static void dwc3_free_trb_pool(struct dwc3_ep *dep) 503 { 504 struct dwc3 *dwc = dep->dwc; 505 506 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, 507 dep->trb_pool, dep->trb_pool_dma); 508 509 dep->trb_pool = NULL; 510 dep->trb_pool_dma = 0; 511 } 512 513 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep) 514 { 515 struct dwc3_gadget_ep_cmd_params params; 516 int ret; 517 518 if (dep->flags & DWC3_EP_RESOURCE_ALLOCATED) 519 return 0; 520 521 memset(¶ms, 0x00, sizeof(params)); 522 523 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); 524 525 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, 526 ¶ms); 527 if (ret) 528 return ret; 529 530 dep->flags |= DWC3_EP_RESOURCE_ALLOCATED; 531 return 0; 532 } 533 534 /** 535 * dwc3_gadget_start_config - reset endpoint resources 536 * @dwc: pointer to the DWC3 context 537 * @resource_index: DEPSTARTCFG.XferRscIdx value (must be 0 or 2) 538 * 539 * Set resource_index=0 to reset all endpoints' resources allocation. Do this as 540 * part of the power-on/soft-reset initialization. 541 * 542 * Set resource_index=2 to reset only non-control endpoints' resources. Do this 543 * on receiving the SET_CONFIGURATION request or hibernation resume. 544 */ 545 int dwc3_gadget_start_config(struct dwc3 *dwc, unsigned int resource_index) 546 { 547 struct dwc3_gadget_ep_cmd_params params; 548 struct dwc3_ep *dep; 549 u32 cmd; 550 int i; 551 int ret; 552 553 if (resource_index != 0 && resource_index != 2) 554 return -EINVAL; 555 556 memset(¶ms, 0x00, sizeof(params)); 557 cmd = DWC3_DEPCMD_DEPSTARTCFG; 558 cmd |= DWC3_DEPCMD_PARAM(resource_index); 559 560 ret = dwc3_send_gadget_ep_cmd(dwc->eps[0], cmd, ¶ms); 561 if (ret) 562 return ret; 563 564 /* Reset resource allocation flags */ 565 for (i = resource_index; i < dwc->num_eps; i++) { 566 dep = dwc->eps[i]; 567 if (!dep) 568 continue; 569 570 dep->flags &= ~DWC3_EP_RESOURCE_ALLOCATED; 571 } 572 573 return 0; 574 } 575 576 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action) 577 { 578 const struct usb_ss_ep_comp_descriptor *comp_desc; 579 const struct usb_endpoint_descriptor *desc; 580 struct dwc3_gadget_ep_cmd_params params; 581 struct dwc3 *dwc = dep->dwc; 582 583 comp_desc = dep->endpoint.comp_desc; 584 desc = dep->endpoint.desc; 585 586 memset(¶ms, 0x00, sizeof(params)); 587 588 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) 589 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); 590 591 /* Burst size is only needed in SuperSpeed mode */ 592 if (dwc->gadget->speed >= USB_SPEED_SUPER) { 593 u32 burst = dep->endpoint.maxburst; 594 595 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); 596 } 597 598 params.param0 |= action; 599 if (action == DWC3_DEPCFG_ACTION_RESTORE) 600 params.param2 |= dep->saved_state; 601 602 if (usb_endpoint_xfer_control(desc)) 603 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; 604 605 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) 606 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; 607 608 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { 609 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE 610 | DWC3_DEPCFG_XFER_COMPLETE_EN 611 | DWC3_DEPCFG_STREAM_EVENT_EN; 612 dep->stream_capable = true; 613 } 614 615 if (!usb_endpoint_xfer_control(desc)) 616 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; 617 618 /* 619 * We are doing 1:1 mapping for endpoints, meaning 620 * Physical Endpoints 2 maps to Logical Endpoint 2 and 621 * so on. We consider the direction bit as part of the physical 622 * endpoint number. So USB endpoint 0x81 is 0x03. 623 */ 624 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); 625 626 /* 627 * We must use the lower 16 TX FIFOs even though 628 * HW might have more 629 */ 630 if (dep->direction) 631 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); 632 633 if (desc->bInterval) { 634 u8 bInterval_m1; 635 636 /* 637 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13. 638 * 639 * NOTE: The programming guide incorrectly stated bInterval_m1 640 * must be set to 0 when operating in fullspeed. Internally the 641 * controller does not have this limitation. See DWC_usb3x 642 * programming guide section 3.2.2.1. 643 */ 644 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13); 645 646 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT && 647 dwc->gadget->speed == USB_SPEED_FULL) 648 dep->interval = desc->bInterval; 649 else 650 dep->interval = 1 << (desc->bInterval - 1); 651 652 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1); 653 } 654 655 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); 656 } 657 658 /** 659 * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value 660 * @dwc: pointer to the DWC3 context 661 * @mult: multiplier to be used when calculating the fifo_size 662 * 663 * Calculates the size value based on the equation below: 664 * 665 * DWC3 revision 280A and prior: 666 * fifo_size = mult * (max_packet / mdwidth) + 1; 667 * 668 * DWC3 revision 290A and onwards: 669 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 670 * 671 * The max packet size is set to 1024, as the txfifo requirements mainly apply 672 * to super speed USB use cases. However, it is safe to overestimate the fifo 673 * allocations for other scenarios, i.e. high speed USB. 674 */ 675 static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult) 676 { 677 int max_packet = 1024; 678 int fifo_size; 679 int mdwidth; 680 681 mdwidth = dwc3_mdwidth(dwc); 682 683 /* MDWIDTH is represented in bits, we need it in bytes */ 684 mdwidth >>= 3; 685 686 if (DWC3_VER_IS_PRIOR(DWC3, 290A)) 687 fifo_size = mult * (max_packet / mdwidth) + 1; 688 else 689 fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1; 690 return fifo_size; 691 } 692 693 /** 694 * dwc3_gadget_calc_ram_depth - calculates the ram depth for txfifo 695 * @dwc: pointer to the DWC3 context 696 */ 697 static int dwc3_gadget_calc_ram_depth(struct dwc3 *dwc) 698 { 699 int ram_depth; 700 int fifo_0_start; 701 bool is_single_port_ram; 702 703 /* Check supporting RAM type by HW */ 704 is_single_port_ram = DWC3_SPRAM_TYPE(dwc->hwparams.hwparams1); 705 706 /* 707 * If a single port RAM is utilized, then allocate TxFIFOs from 708 * RAM0. otherwise, allocate them from RAM1. 709 */ 710 ram_depth = is_single_port_ram ? DWC3_RAM0_DEPTH(dwc->hwparams.hwparams6) : 711 DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); 712 713 /* 714 * In a single port RAM configuration, the available RAM is shared 715 * between the RX and TX FIFOs. This means that the txfifo can begin 716 * at a non-zero address. 717 */ 718 if (is_single_port_ram) { 719 u32 reg; 720 721 /* Check if TXFIFOs start at non-zero addr */ 722 reg = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); 723 fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(reg); 724 725 ram_depth -= (fifo_0_start >> 16); 726 } 727 728 return ram_depth; 729 } 730 731 /** 732 * dwc3_gadget_clear_tx_fifos - Clears txfifo allocation 733 * @dwc: pointer to the DWC3 context 734 * 735 * Iterates through all the endpoint registers and clears the previous txfifo 736 * allocations. 737 */ 738 void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc) 739 { 740 struct dwc3_ep *dep; 741 int fifo_depth; 742 int size; 743 int num; 744 745 if (!dwc->do_fifo_resize) 746 return; 747 748 /* Read ep0IN related TXFIFO size */ 749 dep = dwc->eps[1]; 750 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); 751 if (DWC3_IP_IS(DWC3)) 752 fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size); 753 else 754 fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size); 755 756 dwc->last_fifo_depth = fifo_depth; 757 /* Clear existing TXFIFO for all IN eps except ep0 */ 758 for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM); num += 2) { 759 dep = dwc->eps[num]; 760 if (!dep) 761 continue; 762 763 /* Don't change TXFRAMNUM on usb31 version */ 764 size = DWC3_IP_IS(DWC3) ? 0 : 765 dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) & 766 DWC31_GTXFIFOSIZ_TXFRAMNUM; 767 768 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size); 769 dep->flags &= ~DWC3_EP_TXFIFO_RESIZED; 770 } 771 dwc->num_ep_resized = 0; 772 } 773 774 /* 775 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case 776 * @dwc: pointer to our context structure 777 * 778 * This function will a best effort FIFO allocation in order 779 * to improve FIFO usage and throughput, while still allowing 780 * us to enable as many endpoints as possible. 781 * 782 * Keep in mind that this operation will be highly dependent 783 * on the configured size for RAM1 - which contains TxFifo -, 784 * the amount of endpoints enabled on coreConsultant tool, and 785 * the width of the Master Bus. 786 * 787 * In general, FIFO depths are represented with the following equation: 788 * 789 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 790 * 791 * In conjunction with dwc3_gadget_check_config(), this resizing logic will 792 * ensure that all endpoints will have enough internal memory for one max 793 * packet per endpoint. 794 */ 795 static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep) 796 { 797 struct dwc3 *dwc = dep->dwc; 798 int fifo_0_start; 799 int ram_depth; 800 int fifo_size; 801 int min_depth; 802 int num_in_ep; 803 int remaining; 804 int num_fifos = 1; 805 int fifo; 806 int tmp; 807 808 if (!dwc->do_fifo_resize) 809 return 0; 810 811 /* resize IN endpoints except ep0 */ 812 if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1) 813 return 0; 814 815 /* bail if already resized */ 816 if (dep->flags & DWC3_EP_TXFIFO_RESIZED) 817 return 0; 818 819 ram_depth = dwc3_gadget_calc_ram_depth(dwc); 820 821 switch (dwc->gadget->speed) { 822 case USB_SPEED_SUPER_PLUS: 823 case USB_SPEED_SUPER: 824 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) || 825 usb_endpoint_xfer_isoc(dep->endpoint.desc)) 826 num_fifos = min_t(unsigned int, 827 dep->endpoint.maxburst, 828 dwc->tx_fifo_resize_max_num); 829 break; 830 case USB_SPEED_HIGH: 831 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 832 num_fifos = min_t(unsigned int, 833 usb_endpoint_maxp_mult(dep->endpoint.desc) + 1, 834 dwc->tx_fifo_resize_max_num); 835 break; 836 } 837 fallthrough; 838 case USB_SPEED_FULL: 839 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)) 840 num_fifos = 2; 841 break; 842 default: 843 break; 844 } 845 846 /* FIFO size for a single buffer */ 847 fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1); 848 849 /* Calculate the number of remaining EPs w/o any FIFO */ 850 num_in_ep = dwc->max_cfg_eps; 851 num_in_ep -= dwc->num_ep_resized; 852 853 /* Reserve at least one FIFO for the number of IN EPs */ 854 min_depth = num_in_ep * (fifo + 1); 855 remaining = ram_depth - min_depth - dwc->last_fifo_depth; 856 remaining = max_t(int, 0, remaining); 857 /* 858 * We've already reserved 1 FIFO per EP, so check what we can fit in 859 * addition to it. If there is not enough remaining space, allocate 860 * all the remaining space to the EP. 861 */ 862 fifo_size = (num_fifos - 1) * fifo; 863 if (remaining < fifo_size) 864 fifo_size = remaining; 865 866 fifo_size += fifo; 867 /* Last increment according to the TX FIFO size equation */ 868 fifo_size++; 869 870 /* Check if TXFIFOs start at non-zero addr */ 871 tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); 872 fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp); 873 874 fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16)); 875 if (DWC3_IP_IS(DWC3)) 876 dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); 877 else 878 dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); 879 880 /* Check fifo size allocation doesn't exceed available RAM size. */ 881 if (dwc->last_fifo_depth >= ram_depth) { 882 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n", 883 dwc->last_fifo_depth, ram_depth, 884 dep->endpoint.name, fifo_size); 885 if (DWC3_IP_IS(DWC3)) 886 fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); 887 else 888 fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); 889 890 dwc->last_fifo_depth -= fifo_size; 891 return -ENOMEM; 892 } 893 894 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size); 895 dep->flags |= DWC3_EP_TXFIFO_RESIZED; 896 dwc->num_ep_resized++; 897 898 return 0; 899 } 900 901 /** 902 * __dwc3_gadget_ep_enable - initializes a hw endpoint 903 * @dep: endpoint to be initialized 904 * @action: one of INIT, MODIFY or RESTORE 905 * 906 * Caller should take care of locking. Execute all necessary commands to 907 * initialize a HW endpoint so it can be used by a gadget driver. 908 */ 909 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action) 910 { 911 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 912 struct dwc3 *dwc = dep->dwc; 913 914 u32 reg; 915 int ret; 916 917 if (!(dep->flags & DWC3_EP_ENABLED)) { 918 ret = dwc3_gadget_resize_tx_fifos(dep); 919 if (ret) 920 return ret; 921 } 922 923 ret = dwc3_gadget_set_ep_config(dep, action); 924 if (ret) 925 return ret; 926 927 if (!(dep->flags & DWC3_EP_RESOURCE_ALLOCATED)) { 928 ret = dwc3_gadget_set_xfer_resource(dep); 929 if (ret) 930 return ret; 931 } 932 933 if (!(dep->flags & DWC3_EP_ENABLED)) { 934 struct dwc3_trb *trb_st_hw; 935 struct dwc3_trb *trb_link; 936 937 dep->type = usb_endpoint_type(desc); 938 dep->flags |= DWC3_EP_ENABLED; 939 940 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); 941 reg |= DWC3_DALEPENA_EP(dep->number); 942 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); 943 944 dep->trb_dequeue = 0; 945 dep->trb_enqueue = 0; 946 947 if (usb_endpoint_xfer_control(desc)) 948 goto out; 949 950 /* Initialize the TRB ring */ 951 memset(dep->trb_pool, 0, 952 sizeof(struct dwc3_trb) * DWC3_TRB_NUM); 953 954 /* Link TRB. The HWO bit is never reset */ 955 trb_st_hw = &dep->trb_pool[0]; 956 957 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; 958 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); 959 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); 960 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; 961 trb_link->ctrl |= DWC3_TRB_CTRL_HWO; 962 } 963 964 /* 965 * Issue StartTransfer here with no-op TRB so we can always rely on No 966 * Response Update Transfer command. 967 */ 968 if (usb_endpoint_xfer_bulk(desc) || 969 usb_endpoint_xfer_int(desc)) { 970 struct dwc3_gadget_ep_cmd_params params; 971 struct dwc3_trb *trb; 972 dma_addr_t trb_dma; 973 u32 cmd; 974 975 memset(¶ms, 0, sizeof(params)); 976 trb = &dep->trb_pool[0]; 977 trb_dma = dwc3_trb_dma_offset(dep, trb); 978 979 params.param0 = upper_32_bits(trb_dma); 980 params.param1 = lower_32_bits(trb_dma); 981 982 cmd = DWC3_DEPCMD_STARTTRANSFER; 983 984 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 985 if (ret < 0) 986 return ret; 987 988 if (dep->stream_capable) { 989 /* 990 * For streams, at start, there maybe a race where the 991 * host primes the endpoint before the function driver 992 * queues a request to initiate a stream. In that case, 993 * the controller will not see the prime to generate the 994 * ERDY and start stream. To workaround this, issue a 995 * no-op TRB as normal, but end it immediately. As a 996 * result, when the function driver queues the request, 997 * the next START_TRANSFER command will cause the 998 * controller to generate an ERDY to initiate the 999 * stream. 1000 */ 1001 dwc3_stop_active_transfer(dep, true, true); 1002 1003 /* 1004 * All stream eps will reinitiate stream on NoStream 1005 * rejection. 1006 * 1007 * However, if the controller is capable of 1008 * TXF_FLUSH_BYPASS, then IN direction endpoints will 1009 * automatically restart the stream without the driver 1010 * initiation. 1011 */ 1012 if (!dep->direction || 1013 !(dwc->hwparams.hwparams9 & 1014 DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS)) 1015 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; 1016 } 1017 } 1018 1019 out: 1020 trace_dwc3_gadget_ep_enable(dep); 1021 1022 return 0; 1023 } 1024 1025 void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status) 1026 { 1027 struct dwc3_request *req; 1028 1029 dwc3_stop_active_transfer(dep, true, false); 1030 1031 /* If endxfer is delayed, avoid unmapping requests */ 1032 if (dep->flags & DWC3_EP_DELAY_STOP) 1033 return; 1034 1035 /* - giveback all requests to gadget driver */ 1036 while (!list_empty(&dep->started_list)) { 1037 req = next_request(&dep->started_list); 1038 1039 dwc3_gadget_giveback(dep, req, status); 1040 } 1041 1042 while (!list_empty(&dep->pending_list)) { 1043 req = next_request(&dep->pending_list); 1044 1045 dwc3_gadget_giveback(dep, req, status); 1046 } 1047 1048 while (!list_empty(&dep->cancelled_list)) { 1049 req = next_request(&dep->cancelled_list); 1050 1051 dwc3_gadget_giveback(dep, req, status); 1052 } 1053 } 1054 1055 /** 1056 * __dwc3_gadget_ep_disable - disables a hw endpoint 1057 * @dep: the endpoint to disable 1058 * 1059 * This function undoes what __dwc3_gadget_ep_enable did and also removes 1060 * requests which are currently being processed by the hardware and those which 1061 * are not yet scheduled. 1062 * 1063 * Caller should take care of locking. 1064 */ 1065 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) 1066 { 1067 struct dwc3 *dwc = dep->dwc; 1068 u32 reg; 1069 u32 mask; 1070 1071 trace_dwc3_gadget_ep_disable(dep); 1072 1073 /* make sure HW endpoint isn't stalled */ 1074 if (dep->flags & DWC3_EP_STALL) 1075 __dwc3_gadget_ep_set_halt(dep, 0, false); 1076 1077 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); 1078 reg &= ~DWC3_DALEPENA_EP(dep->number); 1079 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); 1080 1081 dwc3_remove_requests(dwc, dep, -ESHUTDOWN); 1082 1083 dep->stream_capable = false; 1084 dep->type = 0; 1085 mask = DWC3_EP_TXFIFO_RESIZED | DWC3_EP_RESOURCE_ALLOCATED; 1086 /* 1087 * dwc3_remove_requests() can exit early if DWC3 EP delayed stop is 1088 * set. Do not clear DEP flags, so that the end transfer command will 1089 * be reattempted during the next SETUP stage. 1090 */ 1091 if (dep->flags & DWC3_EP_DELAY_STOP) 1092 mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED); 1093 dep->flags &= mask; 1094 1095 /* Clear out the ep descriptors for non-ep0 */ 1096 if (dep->number > 1) { 1097 dep->endpoint.comp_desc = NULL; 1098 dep->endpoint.desc = NULL; 1099 } 1100 1101 return 0; 1102 } 1103 1104 /* -------------------------------------------------------------------------- */ 1105 1106 static int dwc3_gadget_ep0_enable(struct usb_ep *ep, 1107 const struct usb_endpoint_descriptor *desc) 1108 { 1109 return -EINVAL; 1110 } 1111 1112 static int dwc3_gadget_ep0_disable(struct usb_ep *ep) 1113 { 1114 return -EINVAL; 1115 } 1116 1117 /* -------------------------------------------------------------------------- */ 1118 1119 static int dwc3_gadget_ep_enable(struct usb_ep *ep, 1120 const struct usb_endpoint_descriptor *desc) 1121 { 1122 struct dwc3_ep *dep; 1123 struct dwc3 *dwc; 1124 unsigned long flags; 1125 int ret; 1126 1127 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { 1128 pr_debug("dwc3: invalid parameters\n"); 1129 return -EINVAL; 1130 } 1131 1132 if (!desc->wMaxPacketSize) { 1133 pr_debug("dwc3: missing wMaxPacketSize\n"); 1134 return -EINVAL; 1135 } 1136 1137 dep = to_dwc3_ep(ep); 1138 dwc = dep->dwc; 1139 1140 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, 1141 "%s is already enabled\n", 1142 dep->name)) 1143 return 0; 1144 1145 spin_lock_irqsave(&dwc->lock, flags); 1146 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 1147 spin_unlock_irqrestore(&dwc->lock, flags); 1148 1149 return ret; 1150 } 1151 1152 static int dwc3_gadget_ep_disable(struct usb_ep *ep) 1153 { 1154 struct dwc3_ep *dep; 1155 struct dwc3 *dwc; 1156 unsigned long flags; 1157 int ret; 1158 1159 if (!ep) { 1160 pr_debug("dwc3: invalid parameters\n"); 1161 return -EINVAL; 1162 } 1163 1164 dep = to_dwc3_ep(ep); 1165 dwc = dep->dwc; 1166 1167 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), 1168 "%s is already disabled\n", 1169 dep->name)) 1170 return 0; 1171 1172 spin_lock_irqsave(&dwc->lock, flags); 1173 ret = __dwc3_gadget_ep_disable(dep); 1174 spin_unlock_irqrestore(&dwc->lock, flags); 1175 1176 return ret; 1177 } 1178 1179 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, 1180 gfp_t gfp_flags) 1181 { 1182 struct dwc3_request *req; 1183 struct dwc3_ep *dep = to_dwc3_ep(ep); 1184 1185 req = kzalloc(sizeof(*req), gfp_flags); 1186 if (!req) 1187 return NULL; 1188 1189 req->direction = dep->direction; 1190 req->epnum = dep->number; 1191 req->dep = dep; 1192 req->status = DWC3_REQUEST_STATUS_UNKNOWN; 1193 1194 trace_dwc3_alloc_request(req); 1195 1196 return &req->request; 1197 } 1198 1199 static void dwc3_gadget_ep_free_request(struct usb_ep *ep, 1200 struct usb_request *request) 1201 { 1202 struct dwc3_request *req = to_dwc3_request(request); 1203 1204 trace_dwc3_free_request(req); 1205 kfree(req); 1206 } 1207 1208 /** 1209 * dwc3_ep_prev_trb - returns the previous TRB in the ring 1210 * @dep: The endpoint with the TRB ring 1211 * @index: The index of the current TRB in the ring 1212 * 1213 * Returns the TRB prior to the one pointed to by the index. If the 1214 * index is 0, we will wrap backwards, skip the link TRB, and return 1215 * the one just before that. 1216 */ 1217 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) 1218 { 1219 u8 tmp = index; 1220 1221 if (!tmp) 1222 tmp = DWC3_TRB_NUM - 1; 1223 1224 return &dep->trb_pool[tmp - 1]; 1225 } 1226 1227 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) 1228 { 1229 u8 trbs_left; 1230 1231 /* 1232 * If the enqueue & dequeue are equal then the TRB ring is either full 1233 * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs 1234 * pending to be processed by the driver. 1235 */ 1236 if (dep->trb_enqueue == dep->trb_dequeue) { 1237 struct dwc3_request *req; 1238 1239 /* 1240 * If there is any request remained in the started_list with 1241 * active TRBs at this point, then there is no TRB available. 1242 */ 1243 req = next_request(&dep->started_list); 1244 if (req && req->num_trbs) 1245 return 0; 1246 1247 return DWC3_TRB_NUM - 1; 1248 } 1249 1250 trbs_left = dep->trb_dequeue - dep->trb_enqueue; 1251 trbs_left &= (DWC3_TRB_NUM - 1); 1252 1253 if (dep->trb_dequeue < dep->trb_enqueue) 1254 trbs_left--; 1255 1256 return trbs_left; 1257 } 1258 1259 /** 1260 * dwc3_prepare_one_trb - setup one TRB from one request 1261 * @dep: endpoint for which this request is prepared 1262 * @req: dwc3_request pointer 1263 * @trb_length: buffer size of the TRB 1264 * @chain: should this TRB be chained to the next? 1265 * @node: only for isochronous endpoints. First TRB needs different type. 1266 * @use_bounce_buffer: set to use bounce buffer 1267 * @must_interrupt: set to interrupt on TRB completion 1268 */ 1269 static void dwc3_prepare_one_trb(struct dwc3_ep *dep, 1270 struct dwc3_request *req, unsigned int trb_length, 1271 unsigned int chain, unsigned int node, bool use_bounce_buffer, 1272 bool must_interrupt) 1273 { 1274 struct dwc3_trb *trb; 1275 dma_addr_t dma; 1276 unsigned int stream_id = req->request.stream_id; 1277 unsigned int short_not_ok = req->request.short_not_ok; 1278 unsigned int no_interrupt = req->request.no_interrupt; 1279 unsigned int is_last = req->request.is_last; 1280 struct dwc3 *dwc = dep->dwc; 1281 struct usb_gadget *gadget = dwc->gadget; 1282 enum usb_device_speed speed = gadget->speed; 1283 1284 if (use_bounce_buffer) 1285 dma = dep->dwc->bounce_addr; 1286 else if (req->request.num_sgs > 0) 1287 dma = sg_dma_address(req->start_sg); 1288 else 1289 dma = req->request.dma; 1290 1291 trb = &dep->trb_pool[dep->trb_enqueue]; 1292 1293 if (!req->trb) { 1294 dwc3_gadget_move_started_request(req); 1295 req->trb = trb; 1296 req->trb_dma = dwc3_trb_dma_offset(dep, trb); 1297 } 1298 1299 req->num_trbs++; 1300 1301 trb->size = DWC3_TRB_SIZE_LENGTH(trb_length); 1302 trb->bpl = lower_32_bits(dma); 1303 trb->bph = upper_32_bits(dma); 1304 1305 switch (usb_endpoint_type(dep->endpoint.desc)) { 1306 case USB_ENDPOINT_XFER_CONTROL: 1307 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; 1308 break; 1309 1310 case USB_ENDPOINT_XFER_ISOC: 1311 if (!node) { 1312 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; 1313 1314 /* 1315 * USB Specification 2.0 Section 5.9.2 states that: "If 1316 * there is only a single transaction in the microframe, 1317 * only a DATA0 data packet PID is used. If there are 1318 * two transactions per microframe, DATA1 is used for 1319 * the first transaction data packet and DATA0 is used 1320 * for the second transaction data packet. If there are 1321 * three transactions per microframe, DATA2 is used for 1322 * the first transaction data packet, DATA1 is used for 1323 * the second, and DATA0 is used for the third." 1324 * 1325 * IOW, we should satisfy the following cases: 1326 * 1327 * 1) length <= maxpacket 1328 * - DATA0 1329 * 1330 * 2) maxpacket < length <= (2 * maxpacket) 1331 * - DATA1, DATA0 1332 * 1333 * 3) (2 * maxpacket) < length <= (3 * maxpacket) 1334 * - DATA2, DATA1, DATA0 1335 */ 1336 if (speed == USB_SPEED_HIGH) { 1337 struct usb_ep *ep = &dep->endpoint; 1338 unsigned int mult = 2; 1339 unsigned int maxp = usb_endpoint_maxp(ep->desc); 1340 1341 if (req->request.length <= (2 * maxp)) 1342 mult--; 1343 1344 if (req->request.length <= maxp) 1345 mult--; 1346 1347 trb->size |= DWC3_TRB_SIZE_PCM1(mult); 1348 } 1349 } else { 1350 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; 1351 } 1352 1353 if (!no_interrupt && !chain) 1354 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 1355 break; 1356 1357 case USB_ENDPOINT_XFER_BULK: 1358 case USB_ENDPOINT_XFER_INT: 1359 trb->ctrl = DWC3_TRBCTL_NORMAL; 1360 break; 1361 default: 1362 /* 1363 * This is only possible with faulty memory because we 1364 * checked it already :) 1365 */ 1366 dev_WARN(dwc->dev, "Unknown endpoint type %d\n", 1367 usb_endpoint_type(dep->endpoint.desc)); 1368 } 1369 1370 /* 1371 * Enable Continue on Short Packet 1372 * when endpoint is not a stream capable 1373 */ 1374 if (usb_endpoint_dir_out(dep->endpoint.desc)) { 1375 if (!dep->stream_capable) 1376 trb->ctrl |= DWC3_TRB_CTRL_CSP; 1377 1378 if (short_not_ok) 1379 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 1380 } 1381 1382 /* All TRBs setup for MST must set CSP=1 when LST=0 */ 1383 if (dep->stream_capable && DWC3_MST_CAPABLE(&dwc->hwparams)) 1384 trb->ctrl |= DWC3_TRB_CTRL_CSP; 1385 1386 if ((!no_interrupt && !chain) || must_interrupt) 1387 trb->ctrl |= DWC3_TRB_CTRL_IOC; 1388 1389 if (chain) 1390 trb->ctrl |= DWC3_TRB_CTRL_CHN; 1391 else if (dep->stream_capable && is_last && 1392 !DWC3_MST_CAPABLE(&dwc->hwparams)) 1393 trb->ctrl |= DWC3_TRB_CTRL_LST; 1394 1395 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) 1396 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); 1397 1398 /* 1399 * As per data book 4.2.3.2TRB Control Bit Rules section 1400 * 1401 * The controller autonomously checks the HWO field of a TRB to determine if the 1402 * entire TRB is valid. Therefore, software must ensure that the rest of the TRB 1403 * is valid before setting the HWO field to '1'. In most systems, this means that 1404 * software must update the fourth DWORD of a TRB last. 1405 * 1406 * However there is a possibility of CPU re-ordering here which can cause 1407 * controller to observe the HWO bit set prematurely. 1408 * Add a write memory barrier to prevent CPU re-ordering. 1409 */ 1410 wmb(); 1411 trb->ctrl |= DWC3_TRB_CTRL_HWO; 1412 1413 dwc3_ep_inc_enq(dep); 1414 1415 trace_dwc3_prepare_trb(dep, trb); 1416 } 1417 1418 static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req) 1419 { 1420 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); 1421 unsigned int rem = req->request.length % maxp; 1422 1423 if ((req->request.length && req->request.zero && !rem && 1424 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) || 1425 (!req->direction && rem)) 1426 return true; 1427 1428 return false; 1429 } 1430 1431 /** 1432 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry 1433 * @dep: The endpoint that the request belongs to 1434 * @req: The request to prepare 1435 * @entry_length: The last SG entry size 1436 * @node: Indicates whether this is not the first entry (for isoc only) 1437 * 1438 * Return the number of TRBs prepared. 1439 */ 1440 static int dwc3_prepare_last_sg(struct dwc3_ep *dep, 1441 struct dwc3_request *req, unsigned int entry_length, 1442 unsigned int node) 1443 { 1444 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); 1445 unsigned int rem = req->request.length % maxp; 1446 unsigned int num_trbs = 1; 1447 bool needs_extra_trb; 1448 1449 if (dwc3_needs_extra_trb(dep, req)) 1450 num_trbs++; 1451 1452 if (dwc3_calc_trbs_left(dep) < num_trbs) 1453 return 0; 1454 1455 needs_extra_trb = num_trbs > 1; 1456 1457 /* Prepare a normal TRB */ 1458 if (req->direction || req->request.length) 1459 dwc3_prepare_one_trb(dep, req, entry_length, 1460 needs_extra_trb, node, false, false); 1461 1462 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */ 1463 if ((!req->direction && !req->request.length) || needs_extra_trb) 1464 dwc3_prepare_one_trb(dep, req, 1465 req->direction ? 0 : maxp - rem, 1466 false, 1, true, false); 1467 1468 return num_trbs; 1469 } 1470 1471 static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep, 1472 struct dwc3_request *req) 1473 { 1474 struct scatterlist *sg = req->start_sg; 1475 struct scatterlist *s; 1476 int i; 1477 unsigned int length = req->request.length; 1478 unsigned int remaining = req->num_pending_sgs; 1479 unsigned int num_queued_sgs = req->request.num_mapped_sgs - remaining; 1480 unsigned int num_trbs = req->num_trbs; 1481 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req); 1482 1483 /* 1484 * If we resume preparing the request, then get the remaining length of 1485 * the request and resume where we left off. 1486 */ 1487 for_each_sg(req->request.sg, s, num_queued_sgs, i) 1488 length -= sg_dma_len(s); 1489 1490 for_each_sg(sg, s, remaining, i) { 1491 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep); 1492 unsigned int trb_length; 1493 bool must_interrupt = false; 1494 bool last_sg = false; 1495 1496 trb_length = min_t(unsigned int, length, sg_dma_len(s)); 1497 1498 length -= trb_length; 1499 1500 /* 1501 * IOMMU driver is coalescing the list of sgs which shares a 1502 * page boundary into one and giving it to USB driver. With 1503 * this the number of sgs mapped is not equal to the number of 1504 * sgs passed. So mark the chain bit to false if it isthe last 1505 * mapped sg. 1506 */ 1507 if ((i == remaining - 1) || !length) 1508 last_sg = true; 1509 1510 if (!num_trbs_left) 1511 break; 1512 1513 if (last_sg) { 1514 if (!dwc3_prepare_last_sg(dep, req, trb_length, i)) 1515 break; 1516 } else { 1517 /* 1518 * Look ahead to check if we have enough TRBs for the 1519 * next SG entry. If not, set interrupt on this TRB to 1520 * resume preparing the next SG entry when more TRBs are 1521 * free. 1522 */ 1523 if (num_trbs_left == 1 || (needs_extra_trb && 1524 num_trbs_left <= 2 && 1525 sg_dma_len(sg_next(s)) >= length)) { 1526 struct dwc3_request *r; 1527 1528 /* Check if previous requests already set IOC */ 1529 list_for_each_entry(r, &dep->started_list, list) { 1530 if (r != req && !r->request.no_interrupt) 1531 break; 1532 1533 if (r == req) 1534 must_interrupt = true; 1535 } 1536 } 1537 1538 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false, 1539 must_interrupt); 1540 } 1541 1542 /* 1543 * There can be a situation where all sgs in sglist are not 1544 * queued because of insufficient trb number. To handle this 1545 * case, update start_sg to next sg to be queued, so that 1546 * we have free trbs we can continue queuing from where we 1547 * previously stopped 1548 */ 1549 if (!last_sg) 1550 req->start_sg = sg_next(s); 1551 1552 req->num_pending_sgs--; 1553 1554 /* 1555 * The number of pending SG entries may not correspond to the 1556 * number of mapped SG entries. If all the data are queued, then 1557 * don't include unused SG entries. 1558 */ 1559 if (length == 0) { 1560 req->num_pending_sgs = 0; 1561 break; 1562 } 1563 1564 if (must_interrupt) 1565 break; 1566 } 1567 1568 return req->num_trbs - num_trbs; 1569 } 1570 1571 static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep, 1572 struct dwc3_request *req) 1573 { 1574 return dwc3_prepare_last_sg(dep, req, req->request.length, 0); 1575 } 1576 1577 /* 1578 * dwc3_prepare_trbs - setup TRBs from requests 1579 * @dep: endpoint for which requests are being prepared 1580 * 1581 * The function goes through the requests list and sets up TRBs for the 1582 * transfers. The function returns once there are no more TRBs available or 1583 * it runs out of requests. 1584 * 1585 * Returns the number of TRBs prepared or negative errno. 1586 */ 1587 static int dwc3_prepare_trbs(struct dwc3_ep *dep) 1588 { 1589 struct dwc3_request *req, *n; 1590 int ret = 0; 1591 1592 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); 1593 1594 /* 1595 * We can get in a situation where there's a request in the started list 1596 * but there weren't enough TRBs to fully kick it in the first time 1597 * around, so it has been waiting for more TRBs to be freed up. 1598 * 1599 * In that case, we should check if we have a request with pending_sgs 1600 * in the started list and prepare TRBs for that request first, 1601 * otherwise we will prepare TRBs completely out of order and that will 1602 * break things. 1603 */ 1604 list_for_each_entry(req, &dep->started_list, list) { 1605 if (req->num_pending_sgs > 0) { 1606 ret = dwc3_prepare_trbs_sg(dep, req); 1607 if (!ret || req->num_pending_sgs) 1608 return ret; 1609 } 1610 1611 if (!dwc3_calc_trbs_left(dep)) 1612 return ret; 1613 1614 /* 1615 * Don't prepare beyond a transfer. In DWC_usb32, its transfer 1616 * burst capability may try to read and use TRBs beyond the 1617 * active transfer instead of stopping. 1618 */ 1619 if (dep->stream_capable && req->request.is_last && 1620 !DWC3_MST_CAPABLE(&dep->dwc->hwparams)) 1621 return ret; 1622 } 1623 1624 list_for_each_entry_safe(req, n, &dep->pending_list, list) { 1625 struct dwc3 *dwc = dep->dwc; 1626 1627 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, 1628 dep->direction); 1629 if (ret) 1630 return ret; 1631 1632 req->start_sg = req->request.sg; 1633 req->num_pending_sgs = req->request.num_mapped_sgs; 1634 1635 if (req->num_pending_sgs > 0) { 1636 ret = dwc3_prepare_trbs_sg(dep, req); 1637 if (req->num_pending_sgs) 1638 return ret; 1639 } else { 1640 ret = dwc3_prepare_trbs_linear(dep, req); 1641 } 1642 1643 if (!ret || !dwc3_calc_trbs_left(dep)) 1644 return ret; 1645 1646 /* 1647 * Don't prepare beyond a transfer. In DWC_usb32, its transfer 1648 * burst capability may try to read and use TRBs beyond the 1649 * active transfer instead of stopping. 1650 */ 1651 if (dep->stream_capable && req->request.is_last && 1652 !DWC3_MST_CAPABLE(&dwc->hwparams)) 1653 return ret; 1654 } 1655 1656 return ret; 1657 } 1658 1659 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep); 1660 1661 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep) 1662 { 1663 struct dwc3_gadget_ep_cmd_params params; 1664 struct dwc3_request *req; 1665 int starting; 1666 int ret; 1667 u32 cmd; 1668 1669 /* 1670 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0). 1671 * This happens when we need to stop and restart a transfer such as in 1672 * the case of reinitiating a stream or retrying an isoc transfer. 1673 */ 1674 ret = dwc3_prepare_trbs(dep); 1675 if (ret < 0) 1676 return ret; 1677 1678 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); 1679 1680 /* 1681 * If there's no new TRB prepared and we don't need to restart a 1682 * transfer, there's no need to update the transfer. 1683 */ 1684 if (!ret && !starting) 1685 return ret; 1686 1687 req = next_request(&dep->started_list); 1688 if (!req) { 1689 dep->flags |= DWC3_EP_PENDING_REQUEST; 1690 return 0; 1691 } 1692 1693 memset(¶ms, 0, sizeof(params)); 1694 1695 if (starting) { 1696 params.param0 = upper_32_bits(req->trb_dma); 1697 params.param1 = lower_32_bits(req->trb_dma); 1698 cmd = DWC3_DEPCMD_STARTTRANSFER; 1699 1700 if (dep->stream_capable) 1701 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); 1702 1703 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) 1704 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); 1705 } else { 1706 cmd = DWC3_DEPCMD_UPDATETRANSFER | 1707 DWC3_DEPCMD_PARAM(dep->resource_index); 1708 } 1709 1710 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1711 if (ret < 0) { 1712 struct dwc3_request *tmp; 1713 1714 if (ret == -EAGAIN) 1715 return ret; 1716 1717 dwc3_stop_active_transfer(dep, true, true); 1718 1719 list_for_each_entry_safe(req, tmp, &dep->started_list, list) 1720 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED); 1721 1722 /* If ep isn't started, then there's no end transfer pending */ 1723 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) 1724 dwc3_gadget_ep_cleanup_cancelled_requests(dep); 1725 1726 return ret; 1727 } 1728 1729 if (dep->stream_capable && req->request.is_last && 1730 !DWC3_MST_CAPABLE(&dep->dwc->hwparams)) 1731 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE; 1732 1733 return 0; 1734 } 1735 1736 static int __dwc3_gadget_get_frame(struct dwc3 *dwc) 1737 { 1738 u32 reg; 1739 1740 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1741 return DWC3_DSTS_SOFFN(reg); 1742 } 1743 1744 /** 1745 * __dwc3_stop_active_transfer - stop the current active transfer 1746 * @dep: isoc endpoint 1747 * @force: set forcerm bit in the command 1748 * @interrupt: command complete interrupt after End Transfer command 1749 * 1750 * When setting force, the ForceRM bit will be set. In that case 1751 * the controller won't update the TRB progress on command 1752 * completion. It also won't clear the HWO bit in the TRB. 1753 * The command will also not complete immediately in that case. 1754 */ 1755 static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt) 1756 { 1757 struct dwc3_gadget_ep_cmd_params params; 1758 u32 cmd; 1759 int ret; 1760 1761 cmd = DWC3_DEPCMD_ENDTRANSFER; 1762 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; 1763 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0; 1764 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); 1765 memset(¶ms, 0, sizeof(params)); 1766 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1767 /* 1768 * If the End Transfer command was timed out while the device is 1769 * not in SETUP phase, it's possible that an incoming Setup packet 1770 * may prevent the command's completion. Let's retry when the 1771 * ep0state returns to EP0_SETUP_PHASE. 1772 */ 1773 if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) { 1774 dep->flags |= DWC3_EP_DELAY_STOP; 1775 return 0; 1776 } 1777 WARN_ON_ONCE(ret); 1778 dep->resource_index = 0; 1779 1780 if (!interrupt) 1781 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 1782 else if (!ret) 1783 dep->flags |= DWC3_EP_END_TRANSFER_PENDING; 1784 1785 dep->flags &= ~DWC3_EP_DELAY_STOP; 1786 return ret; 1787 } 1788 1789 /** 1790 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number 1791 * @dep: isoc endpoint 1792 * 1793 * This function tests for the correct combination of BIT[15:14] from the 16-bit 1794 * microframe number reported by the XferNotReady event for the future frame 1795 * number to start the isoc transfer. 1796 * 1797 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed 1798 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the 1799 * XferNotReady event are invalid. The driver uses this number to schedule the 1800 * isochronous transfer and passes it to the START TRANSFER command. Because 1801 * this number is invalid, the command may fail. If BIT[15:14] matches the 1802 * internal 16-bit microframe, the START TRANSFER command will pass and the 1803 * transfer will start at the scheduled time, if it is off by 1, the command 1804 * will still pass, but the transfer will start 2 seconds in the future. For all 1805 * other conditions, the START TRANSFER command will fail with bus-expiry. 1806 * 1807 * In order to workaround this issue, we can test for the correct combination of 1808 * BIT[15:14] by sending START TRANSFER commands with different values of 1809 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart 1810 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status. 1811 * As the result, within the 4 possible combinations for BIT[15:14], there will 1812 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful 1813 * command status will result in a 2-second delay start. The smaller BIT[15:14] 1814 * value is the correct combination. 1815 * 1816 * Since there are only 4 outcomes and the results are ordered, we can simply 1817 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to 1818 * deduce the smaller successful combination. 1819 * 1820 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01 1821 * of BIT[15:14]. The correct combination is as follow: 1822 * 1823 * if test0 fails and test1 passes, BIT[15:14] is 'b01 1824 * if test0 fails and test1 fails, BIT[15:14] is 'b10 1825 * if test0 passes and test1 fails, BIT[15:14] is 'b11 1826 * if test0 passes and test1 passes, BIT[15:14] is 'b00 1827 * 1828 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN 1829 * endpoints. 1830 */ 1831 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) 1832 { 1833 int cmd_status = 0; 1834 bool test0; 1835 bool test1; 1836 1837 while (dep->combo_num < 2) { 1838 struct dwc3_gadget_ep_cmd_params params; 1839 u32 test_frame_number; 1840 u32 cmd; 1841 1842 /* 1843 * Check if we can start isoc transfer on the next interval or 1844 * 4 uframes in the future with BIT[15:14] as dep->combo_num 1845 */ 1846 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK; 1847 test_frame_number |= dep->combo_num << 14; 1848 test_frame_number += max_t(u32, 4, dep->interval); 1849 1850 params.param0 = upper_32_bits(dep->dwc->bounce_addr); 1851 params.param1 = lower_32_bits(dep->dwc->bounce_addr); 1852 1853 cmd = DWC3_DEPCMD_STARTTRANSFER; 1854 cmd |= DWC3_DEPCMD_PARAM(test_frame_number); 1855 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1856 1857 /* Redo if some other failure beside bus-expiry is received */ 1858 if (cmd_status && cmd_status != -EAGAIN) { 1859 dep->start_cmd_status = 0; 1860 dep->combo_num = 0; 1861 return 0; 1862 } 1863 1864 /* Store the first test status */ 1865 if (dep->combo_num == 0) 1866 dep->start_cmd_status = cmd_status; 1867 1868 dep->combo_num++; 1869 1870 /* 1871 * End the transfer if the START_TRANSFER command is successful 1872 * to wait for the next XferNotReady to test the command again 1873 */ 1874 if (cmd_status == 0) { 1875 dwc3_stop_active_transfer(dep, true, true); 1876 return 0; 1877 } 1878 } 1879 1880 /* test0 and test1 are both completed at this point */ 1881 test0 = (dep->start_cmd_status == 0); 1882 test1 = (cmd_status == 0); 1883 1884 if (!test0 && test1) 1885 dep->combo_num = 1; 1886 else if (!test0 && !test1) 1887 dep->combo_num = 2; 1888 else if (test0 && !test1) 1889 dep->combo_num = 3; 1890 else if (test0 && test1) 1891 dep->combo_num = 0; 1892 1893 dep->frame_number &= DWC3_FRNUMBER_MASK; 1894 dep->frame_number |= dep->combo_num << 14; 1895 dep->frame_number += max_t(u32, 4, dep->interval); 1896 1897 /* Reinitialize test variables */ 1898 dep->start_cmd_status = 0; 1899 dep->combo_num = 0; 1900 1901 return __dwc3_gadget_kick_transfer(dep); 1902 } 1903 1904 static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) 1905 { 1906 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 1907 struct dwc3 *dwc = dep->dwc; 1908 int ret; 1909 int i; 1910 1911 if (list_empty(&dep->pending_list) && 1912 list_empty(&dep->started_list)) { 1913 dep->flags |= DWC3_EP_PENDING_REQUEST; 1914 return -EAGAIN; 1915 } 1916 1917 if (!dwc->dis_start_transfer_quirk && 1918 (DWC3_VER_IS_PRIOR(DWC31, 170A) || 1919 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) { 1920 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction) 1921 return dwc3_gadget_start_isoc_quirk(dep); 1922 } 1923 1924 if (desc->bInterval <= 14 && 1925 dwc->gadget->speed >= USB_SPEED_HIGH) { 1926 u32 frame = __dwc3_gadget_get_frame(dwc); 1927 bool rollover = frame < 1928 (dep->frame_number & DWC3_FRNUMBER_MASK); 1929 1930 /* 1931 * frame_number is set from XferNotReady and may be already 1932 * out of date. DSTS only provides the lower 14 bit of the 1933 * current frame number. So add the upper two bits of 1934 * frame_number and handle a possible rollover. 1935 * This will provide the correct frame_number unless more than 1936 * rollover has happened since XferNotReady. 1937 */ 1938 1939 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) | 1940 frame; 1941 if (rollover) 1942 dep->frame_number += BIT(14); 1943 } 1944 1945 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) { 1946 int future_interval = i + 1; 1947 1948 /* Give the controller at least 500us to schedule transfers */ 1949 if (desc->bInterval < 3) 1950 future_interval += 3 - desc->bInterval; 1951 1952 dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval); 1953 1954 ret = __dwc3_gadget_kick_transfer(dep); 1955 if (ret != -EAGAIN) 1956 break; 1957 } 1958 1959 /* 1960 * After a number of unsuccessful start attempts due to bus-expiry 1961 * status, issue END_TRANSFER command and retry on the next XferNotReady 1962 * event. 1963 */ 1964 if (ret == -EAGAIN) 1965 ret = __dwc3_stop_active_transfer(dep, false, true); 1966 1967 return ret; 1968 } 1969 1970 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) 1971 { 1972 struct dwc3 *dwc = dep->dwc; 1973 1974 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { 1975 dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", 1976 dep->name); 1977 return -ESHUTDOWN; 1978 } 1979 1980 if (WARN(req->dep != dep, "request %p belongs to '%s'\n", 1981 &req->request, req->dep->name)) 1982 return -EINVAL; 1983 1984 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED, 1985 "%s: request %p already in flight\n", 1986 dep->name, &req->request)) 1987 return -EINVAL; 1988 1989 pm_runtime_get(dwc->dev); 1990 1991 req->request.actual = 0; 1992 req->request.status = -EINPROGRESS; 1993 1994 trace_dwc3_ep_queue(req); 1995 1996 list_add_tail(&req->list, &dep->pending_list); 1997 req->status = DWC3_REQUEST_STATUS_QUEUED; 1998 1999 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE) 2000 return 0; 2001 2002 /* 2003 * Start the transfer only after the END_TRANSFER is completed 2004 * and endpoint STALL is cleared. 2005 */ 2006 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || 2007 (dep->flags & DWC3_EP_WEDGE) || 2008 (dep->flags & DWC3_EP_DELAY_STOP) || 2009 (dep->flags & DWC3_EP_STALL)) { 2010 dep->flags |= DWC3_EP_DELAY_START; 2011 return 0; 2012 } 2013 2014 /* 2015 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must 2016 * wait for a XferNotReady event so we will know what's the current 2017 * (micro-)frame number. 2018 * 2019 * Without this trick, we are very, very likely gonna get Bus Expiry 2020 * errors which will force us issue EndTransfer command. 2021 */ 2022 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 2023 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) { 2024 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) 2025 return __dwc3_gadget_start_isoc(dep); 2026 2027 return 0; 2028 } 2029 } 2030 2031 __dwc3_gadget_kick_transfer(dep); 2032 2033 return 0; 2034 } 2035 2036 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, 2037 gfp_t gfp_flags) 2038 { 2039 struct dwc3_request *req = to_dwc3_request(request); 2040 struct dwc3_ep *dep = to_dwc3_ep(ep); 2041 struct dwc3 *dwc = dep->dwc; 2042 2043 unsigned long flags; 2044 2045 int ret; 2046 2047 spin_lock_irqsave(&dwc->lock, flags); 2048 ret = __dwc3_gadget_ep_queue(dep, req); 2049 spin_unlock_irqrestore(&dwc->lock, flags); 2050 2051 return ret; 2052 } 2053 2054 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req) 2055 { 2056 int i; 2057 2058 /* If req->trb is not set, then the request has not started */ 2059 if (!req->trb) 2060 return; 2061 2062 /* 2063 * If request was already started, this means we had to 2064 * stop the transfer. With that we also need to ignore 2065 * all TRBs used by the request, however TRBs can only 2066 * be modified after completion of END_TRANSFER 2067 * command. So what we do here is that we wait for 2068 * END_TRANSFER completion and only after that, we jump 2069 * over TRBs by clearing HWO and incrementing dequeue 2070 * pointer. 2071 */ 2072 for (i = 0; i < req->num_trbs; i++) { 2073 struct dwc3_trb *trb; 2074 2075 trb = &dep->trb_pool[dep->trb_dequeue]; 2076 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 2077 dwc3_ep_inc_deq(dep); 2078 } 2079 2080 req->num_trbs = 0; 2081 } 2082 2083 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep) 2084 { 2085 struct dwc3_request *req; 2086 struct dwc3 *dwc = dep->dwc; 2087 2088 while (!list_empty(&dep->cancelled_list)) { 2089 req = next_request(&dep->cancelled_list); 2090 dwc3_gadget_ep_skip_trbs(dep, req); 2091 switch (req->status) { 2092 case DWC3_REQUEST_STATUS_DISCONNECTED: 2093 dwc3_gadget_giveback(dep, req, -ESHUTDOWN); 2094 break; 2095 case DWC3_REQUEST_STATUS_DEQUEUED: 2096 dwc3_gadget_giveback(dep, req, -ECONNRESET); 2097 break; 2098 case DWC3_REQUEST_STATUS_STALLED: 2099 dwc3_gadget_giveback(dep, req, -EPIPE); 2100 break; 2101 default: 2102 dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status); 2103 dwc3_gadget_giveback(dep, req, -ECONNRESET); 2104 break; 2105 } 2106 /* 2107 * The endpoint is disabled, let the dwc3_remove_requests() 2108 * handle the cleanup. 2109 */ 2110 if (!dep->endpoint.desc) 2111 break; 2112 } 2113 } 2114 2115 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, 2116 struct usb_request *request) 2117 { 2118 struct dwc3_request *req = to_dwc3_request(request); 2119 struct dwc3_request *r = NULL; 2120 2121 struct dwc3_ep *dep = to_dwc3_ep(ep); 2122 struct dwc3 *dwc = dep->dwc; 2123 2124 unsigned long flags; 2125 int ret = 0; 2126 2127 trace_dwc3_ep_dequeue(req); 2128 2129 spin_lock_irqsave(&dwc->lock, flags); 2130 2131 list_for_each_entry(r, &dep->cancelled_list, list) { 2132 if (r == req) 2133 goto out; 2134 } 2135 2136 list_for_each_entry(r, &dep->pending_list, list) { 2137 if (r == req) { 2138 /* 2139 * Explicitly check for EP0/1 as dequeue for those 2140 * EPs need to be handled differently. Control EP 2141 * only deals with one USB req, and giveback will 2142 * occur during dwc3_ep0_stall_and_restart(). EP0 2143 * requests are never added to started_list. 2144 */ 2145 if (dep->number > 1) 2146 dwc3_gadget_giveback(dep, req, -ECONNRESET); 2147 else 2148 dwc3_ep0_reset_state(dwc); 2149 goto out; 2150 } 2151 } 2152 2153 list_for_each_entry(r, &dep->started_list, list) { 2154 if (r == req) { 2155 struct dwc3_request *t; 2156 2157 /* wait until it is processed */ 2158 dwc3_stop_active_transfer(dep, true, true); 2159 2160 /* 2161 * Remove any started request if the transfer is 2162 * cancelled. 2163 */ 2164 list_for_each_entry_safe(r, t, &dep->started_list, list) 2165 dwc3_gadget_move_cancelled_request(r, 2166 DWC3_REQUEST_STATUS_DEQUEUED); 2167 2168 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; 2169 2170 goto out; 2171 } 2172 } 2173 2174 dev_err(dwc->dev, "request %p was not queued to %s\n", 2175 request, ep->name); 2176 ret = -EINVAL; 2177 out: 2178 spin_unlock_irqrestore(&dwc->lock, flags); 2179 2180 return ret; 2181 } 2182 2183 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) 2184 { 2185 struct dwc3_gadget_ep_cmd_params params; 2186 struct dwc3 *dwc = dep->dwc; 2187 struct dwc3_request *req; 2188 struct dwc3_request *tmp; 2189 int ret; 2190 2191 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 2192 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); 2193 return -EINVAL; 2194 } 2195 2196 memset(¶ms, 0x00, sizeof(params)); 2197 2198 if (value) { 2199 struct dwc3_trb *trb; 2200 2201 unsigned int transfer_in_flight; 2202 unsigned int started; 2203 2204 if (dep->number > 1) 2205 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); 2206 else 2207 trb = &dwc->ep0_trb[dep->trb_enqueue]; 2208 2209 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; 2210 started = !list_empty(&dep->started_list); 2211 2212 if (!protocol && ((dep->direction && transfer_in_flight) || 2213 (!dep->direction && started))) { 2214 return -EAGAIN; 2215 } 2216 2217 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, 2218 ¶ms); 2219 if (ret) 2220 dev_err(dwc->dev, "failed to set STALL on %s\n", 2221 dep->name); 2222 else 2223 dep->flags |= DWC3_EP_STALL; 2224 } else { 2225 /* 2226 * Don't issue CLEAR_STALL command to control endpoints. The 2227 * controller automatically clears the STALL when it receives 2228 * the SETUP token. 2229 */ 2230 if (dep->number <= 1) { 2231 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 2232 return 0; 2233 } 2234 2235 dwc3_stop_active_transfer(dep, true, true); 2236 2237 list_for_each_entry_safe(req, tmp, &dep->started_list, list) 2238 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED); 2239 2240 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING || 2241 (dep->flags & DWC3_EP_DELAY_STOP)) { 2242 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL; 2243 if (protocol) 2244 dwc->clear_stall_protocol = dep->number; 2245 2246 return 0; 2247 } 2248 2249 dwc3_gadget_ep_cleanup_cancelled_requests(dep); 2250 2251 ret = dwc3_send_clear_stall_ep_cmd(dep); 2252 if (ret) { 2253 dev_err(dwc->dev, "failed to clear STALL on %s\n", 2254 dep->name); 2255 return ret; 2256 } 2257 2258 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 2259 2260 if ((dep->flags & DWC3_EP_DELAY_START) && 2261 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) 2262 __dwc3_gadget_kick_transfer(dep); 2263 2264 dep->flags &= ~DWC3_EP_DELAY_START; 2265 } 2266 2267 return ret; 2268 } 2269 2270 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) 2271 { 2272 struct dwc3_ep *dep = to_dwc3_ep(ep); 2273 struct dwc3 *dwc = dep->dwc; 2274 2275 unsigned long flags; 2276 2277 int ret; 2278 2279 spin_lock_irqsave(&dwc->lock, flags); 2280 ret = __dwc3_gadget_ep_set_halt(dep, value, false); 2281 spin_unlock_irqrestore(&dwc->lock, flags); 2282 2283 return ret; 2284 } 2285 2286 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) 2287 { 2288 struct dwc3_ep *dep = to_dwc3_ep(ep); 2289 struct dwc3 *dwc = dep->dwc; 2290 unsigned long flags; 2291 int ret; 2292 2293 spin_lock_irqsave(&dwc->lock, flags); 2294 dep->flags |= DWC3_EP_WEDGE; 2295 2296 if (dep->number == 0 || dep->number == 1) 2297 ret = __dwc3_gadget_ep0_set_halt(ep, 1); 2298 else 2299 ret = __dwc3_gadget_ep_set_halt(dep, 1, false); 2300 spin_unlock_irqrestore(&dwc->lock, flags); 2301 2302 return ret; 2303 } 2304 2305 /* -------------------------------------------------------------------------- */ 2306 2307 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { 2308 .bLength = USB_DT_ENDPOINT_SIZE, 2309 .bDescriptorType = USB_DT_ENDPOINT, 2310 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 2311 }; 2312 2313 static const struct usb_ep_ops dwc3_gadget_ep0_ops = { 2314 .enable = dwc3_gadget_ep0_enable, 2315 .disable = dwc3_gadget_ep0_disable, 2316 .alloc_request = dwc3_gadget_ep_alloc_request, 2317 .free_request = dwc3_gadget_ep_free_request, 2318 .queue = dwc3_gadget_ep0_queue, 2319 .dequeue = dwc3_gadget_ep_dequeue, 2320 .set_halt = dwc3_gadget_ep0_set_halt, 2321 .set_wedge = dwc3_gadget_ep_set_wedge, 2322 }; 2323 2324 static const struct usb_ep_ops dwc3_gadget_ep_ops = { 2325 .enable = dwc3_gadget_ep_enable, 2326 .disable = dwc3_gadget_ep_disable, 2327 .alloc_request = dwc3_gadget_ep_alloc_request, 2328 .free_request = dwc3_gadget_ep_free_request, 2329 .queue = dwc3_gadget_ep_queue, 2330 .dequeue = dwc3_gadget_ep_dequeue, 2331 .set_halt = dwc3_gadget_ep_set_halt, 2332 .set_wedge = dwc3_gadget_ep_set_wedge, 2333 }; 2334 2335 /* -------------------------------------------------------------------------- */ 2336 2337 static void dwc3_gadget_enable_linksts_evts(struct dwc3 *dwc, bool set) 2338 { 2339 u32 reg; 2340 2341 if (DWC3_VER_IS_PRIOR(DWC3, 250A)) 2342 return; 2343 2344 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN); 2345 if (set) 2346 reg |= DWC3_DEVTEN_ULSTCNGEN; 2347 else 2348 reg &= ~DWC3_DEVTEN_ULSTCNGEN; 2349 2350 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); 2351 } 2352 2353 static int dwc3_gadget_get_frame(struct usb_gadget *g) 2354 { 2355 struct dwc3 *dwc = gadget_to_dwc(g); 2356 2357 return __dwc3_gadget_get_frame(dwc); 2358 } 2359 2360 static int __dwc3_gadget_wakeup(struct dwc3 *dwc) 2361 { 2362 int ret; 2363 u32 reg; 2364 2365 u8 link_state; 2366 2367 /* 2368 * According to the Databook Remote wakeup request should 2369 * be issued only when the device is in early suspend state. 2370 * 2371 * We can check that via USB Link State bits in DSTS register. 2372 */ 2373 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 2374 2375 link_state = DWC3_DSTS_USBLNKST(reg); 2376 2377 switch (link_state) { 2378 case DWC3_LINK_STATE_RESET: 2379 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ 2380 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ 2381 case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */ 2382 case DWC3_LINK_STATE_U1: 2383 case DWC3_LINK_STATE_RESUME: 2384 break; 2385 default: 2386 return -EINVAL; 2387 } 2388 2389 dwc3_gadget_enable_linksts_evts(dwc, true); 2390 2391 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); 2392 if (ret < 0) { 2393 dev_err(dwc->dev, "failed to put link in Recovery\n"); 2394 dwc3_gadget_enable_linksts_evts(dwc, false); 2395 return ret; 2396 } 2397 2398 /* Recent versions do this automatically */ 2399 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) { 2400 /* write zeroes to Link Change Request */ 2401 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2402 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 2403 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2404 } 2405 2406 /* 2407 * Since link status change events are enabled we will receive 2408 * an U0 event when wakeup is successful. 2409 */ 2410 return 0; 2411 } 2412 2413 static int dwc3_gadget_wakeup(struct usb_gadget *g) 2414 { 2415 struct dwc3 *dwc = gadget_to_dwc(g); 2416 unsigned long flags; 2417 int ret; 2418 2419 if (!dwc->wakeup_configured) { 2420 dev_err(dwc->dev, "remote wakeup not configured\n"); 2421 return -EINVAL; 2422 } 2423 2424 spin_lock_irqsave(&dwc->lock, flags); 2425 if (!dwc->gadget->wakeup_armed) { 2426 dev_err(dwc->dev, "not armed for remote wakeup\n"); 2427 spin_unlock_irqrestore(&dwc->lock, flags); 2428 return -EINVAL; 2429 } 2430 ret = __dwc3_gadget_wakeup(dwc); 2431 2432 spin_unlock_irqrestore(&dwc->lock, flags); 2433 2434 return ret; 2435 } 2436 2437 static void dwc3_resume_gadget(struct dwc3 *dwc); 2438 2439 static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id) 2440 { 2441 struct dwc3 *dwc = gadget_to_dwc(g); 2442 unsigned long flags; 2443 int ret; 2444 int link_state; 2445 2446 if (!dwc->wakeup_configured) { 2447 dev_err(dwc->dev, "remote wakeup not configured\n"); 2448 return -EINVAL; 2449 } 2450 2451 spin_lock_irqsave(&dwc->lock, flags); 2452 /* 2453 * If the link is in U3, signal for remote wakeup and wait for the 2454 * link to transition to U0 before sending device notification. 2455 */ 2456 link_state = dwc3_gadget_get_link_state(dwc); 2457 if (link_state == DWC3_LINK_STATE_U3) { 2458 dwc->wakeup_pending_funcs |= BIT(intf_id); 2459 ret = __dwc3_gadget_wakeup(dwc); 2460 spin_unlock_irqrestore(&dwc->lock, flags); 2461 return ret; 2462 } 2463 2464 ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_DEV_NOTIFICATION, 2465 DWC3_DGCMDPAR_DN_FUNC_WAKE | 2466 DWC3_DGCMDPAR_INTF_SEL(intf_id)); 2467 if (ret) 2468 dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret); 2469 2470 spin_unlock_irqrestore(&dwc->lock, flags); 2471 2472 return ret; 2473 } 2474 2475 static int dwc3_gadget_set_remote_wakeup(struct usb_gadget *g, int set) 2476 { 2477 struct dwc3 *dwc = gadget_to_dwc(g); 2478 unsigned long flags; 2479 2480 spin_lock_irqsave(&dwc->lock, flags); 2481 dwc->wakeup_configured = !!set; 2482 spin_unlock_irqrestore(&dwc->lock, flags); 2483 2484 return 0; 2485 } 2486 2487 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, 2488 int is_selfpowered) 2489 { 2490 struct dwc3 *dwc = gadget_to_dwc(g); 2491 unsigned long flags; 2492 2493 spin_lock_irqsave(&dwc->lock, flags); 2494 g->is_selfpowered = !!is_selfpowered; 2495 spin_unlock_irqrestore(&dwc->lock, flags); 2496 2497 return 0; 2498 } 2499 2500 static void dwc3_stop_active_transfers(struct dwc3 *dwc) 2501 { 2502 u32 epnum; 2503 2504 for (epnum = 2; epnum < dwc->num_eps; epnum++) { 2505 struct dwc3_ep *dep; 2506 2507 dep = dwc->eps[epnum]; 2508 if (!dep) 2509 continue; 2510 2511 dwc3_remove_requests(dwc, dep, -ESHUTDOWN); 2512 } 2513 } 2514 2515 static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc) 2516 { 2517 enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate; 2518 u32 reg; 2519 2520 if (ssp_rate == USB_SSP_GEN_UNKNOWN) 2521 ssp_rate = dwc->max_ssp_rate; 2522 2523 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2524 reg &= ~DWC3_DCFG_SPEED_MASK; 2525 reg &= ~DWC3_DCFG_NUMLANES(~0); 2526 2527 if (ssp_rate == USB_SSP_GEN_1x2) 2528 reg |= DWC3_DCFG_SUPERSPEED; 2529 else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2) 2530 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2531 2532 if (ssp_rate != USB_SSP_GEN_2x1 && 2533 dwc->max_ssp_rate != USB_SSP_GEN_2x1) 2534 reg |= DWC3_DCFG_NUMLANES(1); 2535 2536 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2537 } 2538 2539 static void __dwc3_gadget_set_speed(struct dwc3 *dwc) 2540 { 2541 enum usb_device_speed speed; 2542 u32 reg; 2543 2544 speed = dwc->gadget_max_speed; 2545 if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed) 2546 speed = dwc->maximum_speed; 2547 2548 if (speed == USB_SPEED_SUPER_PLUS && 2549 DWC3_IP_IS(DWC32)) { 2550 __dwc3_gadget_set_ssp_rate(dwc); 2551 return; 2552 } 2553 2554 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2555 reg &= ~(DWC3_DCFG_SPEED_MASK); 2556 2557 /* 2558 * WORKAROUND: DWC3 revision < 2.20a have an issue 2559 * which would cause metastability state on Run/Stop 2560 * bit if we try to force the IP to USB2-only mode. 2561 * 2562 * Because of that, we cannot configure the IP to any 2563 * speed other than the SuperSpeed 2564 * 2565 * Refers to: 2566 * 2567 * STAR#9000525659: Clock Domain Crossing on DCTL in 2568 * USB 2.0 Mode 2569 */ 2570 if (DWC3_VER_IS_PRIOR(DWC3, 220A) && 2571 !dwc->dis_metastability_quirk) { 2572 reg |= DWC3_DCFG_SUPERSPEED; 2573 } else { 2574 switch (speed) { 2575 case USB_SPEED_FULL: 2576 reg |= DWC3_DCFG_FULLSPEED; 2577 break; 2578 case USB_SPEED_HIGH: 2579 reg |= DWC3_DCFG_HIGHSPEED; 2580 break; 2581 case USB_SPEED_SUPER: 2582 reg |= DWC3_DCFG_SUPERSPEED; 2583 break; 2584 case USB_SPEED_SUPER_PLUS: 2585 if (DWC3_IP_IS(DWC3)) 2586 reg |= DWC3_DCFG_SUPERSPEED; 2587 else 2588 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2589 break; 2590 default: 2591 dev_err(dwc->dev, "invalid speed (%d)\n", speed); 2592 2593 if (DWC3_IP_IS(DWC3)) 2594 reg |= DWC3_DCFG_SUPERSPEED; 2595 else 2596 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2597 } 2598 } 2599 2600 if (DWC3_IP_IS(DWC32) && 2601 speed > USB_SPEED_UNKNOWN && 2602 speed < USB_SPEED_SUPER_PLUS) 2603 reg &= ~DWC3_DCFG_NUMLANES(~0); 2604 2605 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2606 } 2607 2608 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on) 2609 { 2610 u32 reg; 2611 u32 timeout = 2000; 2612 u32 saved_config = 0; 2613 2614 if (pm_runtime_suspended(dwc->dev)) 2615 return 0; 2616 2617 /* 2618 * When operating in USB 2.0 speeds (HS/FS), ensure that 2619 * GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY are cleared before starting 2620 * or stopping the controller. This resolves timeout issues that occur 2621 * during frequent role switches between host and device modes. 2622 * 2623 * Save and clear these settings, then restore them after completing the 2624 * controller start or stop sequence. 2625 * 2626 * This solution was discovered through experimentation as it is not 2627 * mentioned in the dwc3 programming guide. It has been tested on an 2628 * Exynos platforms. 2629 */ 2630 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 2631 if (reg & DWC3_GUSB2PHYCFG_SUSPHY) { 2632 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; 2633 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 2634 } 2635 2636 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { 2637 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; 2638 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; 2639 } 2640 2641 if (saved_config) 2642 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 2643 2644 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2645 if (is_on) { 2646 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) { 2647 reg &= ~DWC3_DCTL_TRGTULST_MASK; 2648 reg |= DWC3_DCTL_TRGTULST_RX_DET; 2649 } 2650 2651 if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) 2652 reg &= ~DWC3_DCTL_KEEP_CONNECT; 2653 reg |= DWC3_DCTL_RUN_STOP; 2654 2655 __dwc3_gadget_set_speed(dwc); 2656 dwc->pullups_connected = true; 2657 } else { 2658 reg &= ~DWC3_DCTL_RUN_STOP; 2659 2660 dwc->pullups_connected = false; 2661 } 2662 2663 dwc3_gadget_dctl_write_safe(dwc, reg); 2664 2665 do { 2666 usleep_range(1000, 2000); 2667 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 2668 reg &= DWC3_DSTS_DEVCTRLHLT; 2669 } while (--timeout && !(!is_on ^ !reg)); 2670 2671 if (saved_config) { 2672 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 2673 reg |= saved_config; 2674 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 2675 } 2676 2677 if (!timeout) 2678 return -ETIMEDOUT; 2679 2680 return 0; 2681 } 2682 2683 static void dwc3_gadget_disable_irq(struct dwc3 *dwc); 2684 static void __dwc3_gadget_stop(struct dwc3 *dwc); 2685 static int __dwc3_gadget_start(struct dwc3 *dwc); 2686 2687 static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc) 2688 { 2689 unsigned long flags; 2690 int ret; 2691 2692 spin_lock_irqsave(&dwc->lock, flags); 2693 if (!dwc->pullups_connected) { 2694 spin_unlock_irqrestore(&dwc->lock, flags); 2695 return 0; 2696 } 2697 2698 dwc->connected = false; 2699 2700 /* 2701 * Attempt to end pending SETUP status phase, and not wait for the 2702 * function to do so. 2703 */ 2704 if (dwc->delayed_status) 2705 dwc3_ep0_send_delayed_status(dwc); 2706 2707 /* 2708 * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a 2709 * Section 4.1.8 Table 4-7, it states that for a device-initiated 2710 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER 2711 * command for any active transfers" before clearing the RunStop 2712 * bit. 2713 */ 2714 dwc3_stop_active_transfers(dwc); 2715 spin_unlock_irqrestore(&dwc->lock, flags); 2716 2717 /* 2718 * Per databook, when we want to stop the gadget, if a control transfer 2719 * is still in process, complete it and get the core into setup phase. 2720 * In case the host is unresponsive to a SETUP transaction, forcefully 2721 * stall the transfer, and move back to the SETUP phase, so that any 2722 * pending endxfers can be executed. 2723 */ 2724 if (dwc->ep0state != EP0_SETUP_PHASE) { 2725 reinit_completion(&dwc->ep0_in_setup); 2726 2727 ret = wait_for_completion_timeout(&dwc->ep0_in_setup, 2728 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); 2729 if (ret == 0) { 2730 dev_warn(dwc->dev, "wait for SETUP phase timed out\n"); 2731 spin_lock_irqsave(&dwc->lock, flags); 2732 dwc3_ep0_reset_state(dwc); 2733 spin_unlock_irqrestore(&dwc->lock, flags); 2734 } 2735 } 2736 2737 /* 2738 * Note: if the GEVNTCOUNT indicates events in the event buffer, the 2739 * driver needs to acknowledge them before the controller can halt. 2740 * Simply let the interrupt handler acknowledges and handle the 2741 * remaining event generated by the controller while polling for 2742 * DSTS.DEVCTLHLT. 2743 */ 2744 ret = dwc3_gadget_run_stop(dwc, false); 2745 2746 /* 2747 * Stop the gadget after controller is halted, so that if needed, the 2748 * events to update EP0 state can still occur while the run/stop 2749 * routine polls for the halted state. DEVTEN is cleared as part of 2750 * gadget stop. 2751 */ 2752 spin_lock_irqsave(&dwc->lock, flags); 2753 __dwc3_gadget_stop(dwc); 2754 spin_unlock_irqrestore(&dwc->lock, flags); 2755 2756 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); 2757 2758 return ret; 2759 } 2760 2761 static int dwc3_gadget_soft_connect(struct dwc3 *dwc) 2762 { 2763 int ret; 2764 2765 /* 2766 * In the Synopsys DWC_usb31 1.90a programming guide section 2767 * 4.1.9, it specifies that for a reconnect after a 2768 * device-initiated disconnect requires a core soft reset 2769 * (DCTL.CSftRst) before enabling the run/stop bit. 2770 */ 2771 ret = dwc3_core_soft_reset(dwc); 2772 if (ret) 2773 return ret; 2774 2775 dwc3_event_buffers_setup(dwc); 2776 __dwc3_gadget_start(dwc); 2777 return dwc3_gadget_run_stop(dwc, true); 2778 } 2779 2780 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) 2781 { 2782 struct dwc3 *dwc = gadget_to_dwc(g); 2783 int ret; 2784 2785 is_on = !!is_on; 2786 2787 dwc->softconnect = is_on; 2788 2789 /* 2790 * Avoid issuing a runtime resume if the device is already in the 2791 * suspended state during gadget disconnect. DWC3 gadget was already 2792 * halted/stopped during runtime suspend. 2793 */ 2794 if (!is_on) { 2795 pm_runtime_barrier(dwc->dev); 2796 if (pm_runtime_suspended(dwc->dev)) 2797 return 0; 2798 } 2799 2800 /* 2801 * Check the return value for successful resume, or error. For a 2802 * successful resume, the DWC3 runtime PM resume routine will handle 2803 * the run stop sequence, so avoid duplicate operations here. 2804 */ 2805 ret = pm_runtime_get_sync(dwc->dev); 2806 if (!ret || ret < 0) { 2807 pm_runtime_put(dwc->dev); 2808 if (ret < 0) 2809 pm_runtime_set_suspended(dwc->dev); 2810 return ret; 2811 } 2812 2813 if (dwc->pullups_connected == is_on) { 2814 pm_runtime_put(dwc->dev); 2815 return 0; 2816 } 2817 2818 synchronize_irq(dwc->irq_gadget); 2819 2820 if (!is_on) 2821 ret = dwc3_gadget_soft_disconnect(dwc); 2822 else 2823 ret = dwc3_gadget_soft_connect(dwc); 2824 2825 pm_runtime_put(dwc->dev); 2826 2827 return ret; 2828 } 2829 2830 static void dwc3_gadget_enable_irq(struct dwc3 *dwc) 2831 { 2832 u32 reg; 2833 2834 /* Enable all but Start and End of Frame IRQs */ 2835 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN | 2836 DWC3_DEVTEN_CMDCMPLTEN | 2837 DWC3_DEVTEN_ERRTICERREN | 2838 DWC3_DEVTEN_WKUPEVTEN | 2839 DWC3_DEVTEN_CONNECTDONEEN | 2840 DWC3_DEVTEN_USBRSTEN | 2841 DWC3_DEVTEN_DISCONNEVTEN); 2842 2843 if (DWC3_VER_IS_PRIOR(DWC3, 250A)) 2844 reg |= DWC3_DEVTEN_ULSTCNGEN; 2845 2846 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */ 2847 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) 2848 reg |= DWC3_DEVTEN_U3L2L1SUSPEN; 2849 2850 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); 2851 } 2852 2853 static void dwc3_gadget_disable_irq(struct dwc3 *dwc) 2854 { 2855 /* mask all interrupts */ 2856 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); 2857 } 2858 2859 static irqreturn_t dwc3_interrupt(int irq, void *_dwc); 2860 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); 2861 2862 /** 2863 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG 2864 * @dwc: pointer to our context structure 2865 * 2866 * The following looks like complex but it's actually very simple. In order to 2867 * calculate the number of packets we can burst at once on OUT transfers, we're 2868 * gonna use RxFIFO size. 2869 * 2870 * To calculate RxFIFO size we need two numbers: 2871 * MDWIDTH = size, in bits, of the internal memory bus 2872 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) 2873 * 2874 * Given these two numbers, the formula is simple: 2875 * 2876 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; 2877 * 2878 * 24 bytes is for 3x SETUP packets 2879 * 16 bytes is a clock domain crossing tolerance 2880 * 2881 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; 2882 */ 2883 static void dwc3_gadget_setup_nump(struct dwc3 *dwc) 2884 { 2885 u32 ram2_depth; 2886 u32 mdwidth; 2887 u32 nump; 2888 u32 reg; 2889 2890 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); 2891 mdwidth = dwc3_mdwidth(dwc); 2892 2893 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; 2894 nump = min_t(u32, nump, 16); 2895 2896 /* update NumP */ 2897 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2898 reg &= ~DWC3_DCFG_NUMP_MASK; 2899 reg |= nump << DWC3_DCFG_NUMP_SHIFT; 2900 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2901 } 2902 2903 static int __dwc3_gadget_start(struct dwc3 *dwc) 2904 { 2905 struct dwc3_ep *dep; 2906 int ret = 0; 2907 u32 reg; 2908 2909 /* 2910 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if 2911 * the core supports IMOD, disable it. 2912 */ 2913 if (dwc->imod_interval) { 2914 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); 2915 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 2916 } else if (dwc3_has_imod(dwc)) { 2917 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); 2918 } 2919 2920 /* 2921 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP 2922 * field instead of letting dwc3 itself calculate that automatically. 2923 * 2924 * This way, we maximize the chances that we'll be able to get several 2925 * bursts of data without going through any sort of endpoint throttling. 2926 */ 2927 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); 2928 if (DWC3_IP_IS(DWC3)) 2929 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; 2930 else 2931 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; 2932 2933 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); 2934 2935 dwc3_gadget_setup_nump(dwc); 2936 2937 /* 2938 * Currently the controller handles single stream only. So, Ignore 2939 * Packet Pending bit for stream selection and don't search for another 2940 * stream if the host sends Data Packet with PP=0 (for OUT direction) or 2941 * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves 2942 * the stream performance. 2943 */ 2944 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2945 reg |= DWC3_DCFG_IGNSTRMPP; 2946 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2947 2948 /* Enable MST by default if the device is capable of MST */ 2949 if (DWC3_MST_CAPABLE(&dwc->hwparams)) { 2950 reg = dwc3_readl(dwc->regs, DWC3_DCFG1); 2951 reg &= ~DWC3_DCFG1_DIS_MST_ENH; 2952 dwc3_writel(dwc->regs, DWC3_DCFG1, reg); 2953 } 2954 2955 /* Start with SuperSpeed Default */ 2956 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 2957 2958 ret = dwc3_gadget_start_config(dwc, 0); 2959 if (ret) { 2960 dev_err(dwc->dev, "failed to config endpoints\n"); 2961 return ret; 2962 } 2963 2964 dep = dwc->eps[0]; 2965 dep->flags = 0; 2966 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 2967 if (ret) { 2968 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2969 goto err0; 2970 } 2971 2972 dep = dwc->eps[1]; 2973 dep->flags = 0; 2974 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 2975 if (ret) { 2976 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2977 goto err1; 2978 } 2979 2980 /* begin to receive SETUP packets */ 2981 dwc->ep0state = EP0_SETUP_PHASE; 2982 dwc->ep0_bounced = false; 2983 dwc->link_state = DWC3_LINK_STATE_SS_DIS; 2984 dwc->delayed_status = false; 2985 dwc3_ep0_out_start(dwc); 2986 2987 dwc3_gadget_enable_irq(dwc); 2988 dwc3_enable_susphy(dwc, true); 2989 2990 return 0; 2991 2992 err1: 2993 __dwc3_gadget_ep_disable(dwc->eps[0]); 2994 2995 err0: 2996 return ret; 2997 } 2998 2999 static int dwc3_gadget_start(struct usb_gadget *g, 3000 struct usb_gadget_driver *driver) 3001 { 3002 struct dwc3 *dwc = gadget_to_dwc(g); 3003 unsigned long flags; 3004 int ret; 3005 int irq; 3006 3007 irq = dwc->irq_gadget; 3008 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, 3009 IRQF_SHARED, "dwc3", dwc->ev_buf); 3010 if (ret) { 3011 dev_err(dwc->dev, "failed to request irq #%d --> %d\n", 3012 irq, ret); 3013 return ret; 3014 } 3015 3016 spin_lock_irqsave(&dwc->lock, flags); 3017 dwc->gadget_driver = driver; 3018 spin_unlock_irqrestore(&dwc->lock, flags); 3019 3020 if (dwc->sys_wakeup) 3021 device_wakeup_enable(dwc->sysdev); 3022 3023 return 0; 3024 } 3025 3026 static void __dwc3_gadget_stop(struct dwc3 *dwc) 3027 { 3028 dwc3_gadget_disable_irq(dwc); 3029 __dwc3_gadget_ep_disable(dwc->eps[0]); 3030 __dwc3_gadget_ep_disable(dwc->eps[1]); 3031 } 3032 3033 static int dwc3_gadget_stop(struct usb_gadget *g) 3034 { 3035 struct dwc3 *dwc = gadget_to_dwc(g); 3036 unsigned long flags; 3037 3038 if (dwc->sys_wakeup) 3039 device_wakeup_disable(dwc->sysdev); 3040 3041 spin_lock_irqsave(&dwc->lock, flags); 3042 dwc->gadget_driver = NULL; 3043 dwc->max_cfg_eps = 0; 3044 spin_unlock_irqrestore(&dwc->lock, flags); 3045 3046 free_irq(dwc->irq_gadget, dwc->ev_buf); 3047 3048 return 0; 3049 } 3050 3051 static void dwc3_gadget_config_params(struct usb_gadget *g, 3052 struct usb_dcd_config_params *params) 3053 { 3054 struct dwc3 *dwc = gadget_to_dwc(g); 3055 3056 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED; 3057 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED; 3058 3059 /* Recommended BESL */ 3060 if (!dwc->dis_enblslpm_quirk) { 3061 /* 3062 * If the recommended BESL baseline is 0 or if the BESL deep is 3063 * less than 2, Microsoft's Windows 10 host usb stack will issue 3064 * a usb reset immediately after it receives the extended BOS 3065 * descriptor and the enumeration will fail. To maintain 3066 * compatibility with the Windows' usb stack, let's set the 3067 * recommended BESL baseline to 1 and clamp the BESL deep to be 3068 * within 2 to 15. 3069 */ 3070 params->besl_baseline = 1; 3071 if (dwc->is_utmi_l1_suspend) 3072 params->besl_deep = 3073 clamp_t(u8, dwc->hird_threshold, 2, 15); 3074 } 3075 3076 /* U1 Device exit Latency */ 3077 if (dwc->dis_u1_entry_quirk) 3078 params->bU1devExitLat = 0; 3079 else 3080 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT; 3081 3082 /* U2 Device exit Latency */ 3083 if (dwc->dis_u2_entry_quirk) 3084 params->bU2DevExitLat = 0; 3085 else 3086 params->bU2DevExitLat = 3087 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT); 3088 } 3089 3090 static void dwc3_gadget_set_speed(struct usb_gadget *g, 3091 enum usb_device_speed speed) 3092 { 3093 struct dwc3 *dwc = gadget_to_dwc(g); 3094 unsigned long flags; 3095 3096 spin_lock_irqsave(&dwc->lock, flags); 3097 dwc->gadget_max_speed = speed; 3098 spin_unlock_irqrestore(&dwc->lock, flags); 3099 } 3100 3101 static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g, 3102 enum usb_ssp_rate rate) 3103 { 3104 struct dwc3 *dwc = gadget_to_dwc(g); 3105 unsigned long flags; 3106 3107 spin_lock_irqsave(&dwc->lock, flags); 3108 dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS; 3109 dwc->gadget_ssp_rate = rate; 3110 spin_unlock_irqrestore(&dwc->lock, flags); 3111 } 3112 3113 static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA) 3114 { 3115 struct dwc3 *dwc = gadget_to_dwc(g); 3116 union power_supply_propval val = {0}; 3117 int ret; 3118 3119 if (dwc->usb2_phy) 3120 return usb_phy_set_power(dwc->usb2_phy, mA); 3121 3122 if (!dwc->usb_psy) 3123 return -EOPNOTSUPP; 3124 3125 val.intval = 1000 * mA; 3126 ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val); 3127 3128 return ret; 3129 } 3130 3131 /** 3132 * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration 3133 * @g: pointer to the USB gadget 3134 * 3135 * Used to record the maximum number of endpoints being used in a USB composite 3136 * device. (across all configurations) This is to be used in the calculation 3137 * of the TXFIFO sizes when resizing internal memory for individual endpoints. 3138 * It will help ensured that the resizing logic reserves enough space for at 3139 * least one max packet. 3140 */ 3141 static int dwc3_gadget_check_config(struct usb_gadget *g) 3142 { 3143 struct dwc3 *dwc = gadget_to_dwc(g); 3144 struct usb_ep *ep; 3145 int fifo_size = 0; 3146 int ram_depth; 3147 int ep_num = 0; 3148 3149 if (!dwc->do_fifo_resize) 3150 return 0; 3151 3152 list_for_each_entry(ep, &g->ep_list, ep_list) { 3153 /* Only interested in the IN endpoints */ 3154 if (ep->claimed && (ep->address & USB_DIR_IN)) 3155 ep_num++; 3156 } 3157 3158 if (ep_num <= dwc->max_cfg_eps) 3159 return 0; 3160 3161 /* Update the max number of eps in the composition */ 3162 dwc->max_cfg_eps = ep_num; 3163 3164 fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps); 3165 /* Based on the equation, increment by one for every ep */ 3166 fifo_size += dwc->max_cfg_eps; 3167 3168 /* Check if we can fit a single fifo per endpoint */ 3169 ram_depth = dwc3_gadget_calc_ram_depth(dwc); 3170 if (fifo_size > ram_depth) 3171 return -ENOMEM; 3172 3173 return 0; 3174 } 3175 3176 static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable) 3177 { 3178 struct dwc3 *dwc = gadget_to_dwc(g); 3179 unsigned long flags; 3180 3181 spin_lock_irqsave(&dwc->lock, flags); 3182 dwc->async_callbacks = enable; 3183 spin_unlock_irqrestore(&dwc->lock, flags); 3184 } 3185 3186 static const struct usb_gadget_ops dwc3_gadget_ops = { 3187 .get_frame = dwc3_gadget_get_frame, 3188 .wakeup = dwc3_gadget_wakeup, 3189 .func_wakeup = dwc3_gadget_func_wakeup, 3190 .set_remote_wakeup = dwc3_gadget_set_remote_wakeup, 3191 .set_selfpowered = dwc3_gadget_set_selfpowered, 3192 .pullup = dwc3_gadget_pullup, 3193 .udc_start = dwc3_gadget_start, 3194 .udc_stop = dwc3_gadget_stop, 3195 .udc_set_speed = dwc3_gadget_set_speed, 3196 .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate, 3197 .get_config_params = dwc3_gadget_config_params, 3198 .vbus_draw = dwc3_gadget_vbus_draw, 3199 .check_config = dwc3_gadget_check_config, 3200 .udc_async_callbacks = dwc3_gadget_async_callbacks, 3201 }; 3202 3203 /* -------------------------------------------------------------------------- */ 3204 3205 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) 3206 { 3207 struct dwc3 *dwc = dep->dwc; 3208 3209 usb_ep_set_maxpacket_limit(&dep->endpoint, 512); 3210 dep->endpoint.maxburst = 1; 3211 dep->endpoint.ops = &dwc3_gadget_ep0_ops; 3212 if (!dep->direction) 3213 dwc->gadget->ep0 = &dep->endpoint; 3214 3215 dep->endpoint.caps.type_control = true; 3216 3217 return 0; 3218 } 3219 3220 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) 3221 { 3222 struct dwc3 *dwc = dep->dwc; 3223 u32 mdwidth; 3224 int size; 3225 int maxpacket; 3226 3227 mdwidth = dwc3_mdwidth(dwc); 3228 3229 /* MDWIDTH is represented in bits, we need it in bytes */ 3230 mdwidth /= 8; 3231 3232 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); 3233 if (DWC3_IP_IS(DWC3)) 3234 size = DWC3_GTXFIFOSIZ_TXFDEP(size); 3235 else 3236 size = DWC31_GTXFIFOSIZ_TXFDEP(size); 3237 3238 /* 3239 * maxpacket size is determined as part of the following, after assuming 3240 * a mult value of one maxpacket: 3241 * DWC3 revision 280A and prior: 3242 * fifo_size = mult * (max_packet / mdwidth) + 1; 3243 * maxpacket = mdwidth * (fifo_size - 1); 3244 * 3245 * DWC3 revision 290A and onwards: 3246 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 3247 * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth; 3248 */ 3249 if (DWC3_VER_IS_PRIOR(DWC3, 290A)) 3250 maxpacket = mdwidth * (size - 1); 3251 else 3252 maxpacket = mdwidth * ((size - 1) - 1) - mdwidth; 3253 3254 /* Functionally, space for one max packet is sufficient */ 3255 size = min_t(int, maxpacket, 1024); 3256 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 3257 3258 dep->endpoint.max_streams = 16; 3259 dep->endpoint.ops = &dwc3_gadget_ep_ops; 3260 list_add_tail(&dep->endpoint.ep_list, 3261 &dwc->gadget->ep_list); 3262 dep->endpoint.caps.type_iso = true; 3263 dep->endpoint.caps.type_bulk = true; 3264 dep->endpoint.caps.type_int = true; 3265 3266 return dwc3_alloc_trb_pool(dep); 3267 } 3268 3269 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) 3270 { 3271 struct dwc3 *dwc = dep->dwc; 3272 u32 mdwidth; 3273 int size; 3274 3275 mdwidth = dwc3_mdwidth(dwc); 3276 3277 /* MDWIDTH is represented in bits, convert to bytes */ 3278 mdwidth /= 8; 3279 3280 /* All OUT endpoints share a single RxFIFO space */ 3281 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0)); 3282 if (DWC3_IP_IS(DWC3)) 3283 size = DWC3_GRXFIFOSIZ_RXFDEP(size); 3284 else 3285 size = DWC31_GRXFIFOSIZ_RXFDEP(size); 3286 3287 /* FIFO depth is in MDWDITH bytes */ 3288 size *= mdwidth; 3289 3290 /* 3291 * To meet performance requirement, a minimum recommended RxFIFO size 3292 * is defined as follow: 3293 * RxFIFO size >= (3 x MaxPacketSize) + 3294 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin) 3295 * 3296 * Then calculate the max packet limit as below. 3297 */ 3298 size -= (3 * 8) + 16; 3299 if (size < 0) 3300 size = 0; 3301 else 3302 size /= 3; 3303 3304 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 3305 dep->endpoint.max_streams = 16; 3306 dep->endpoint.ops = &dwc3_gadget_ep_ops; 3307 list_add_tail(&dep->endpoint.ep_list, 3308 &dwc->gadget->ep_list); 3309 dep->endpoint.caps.type_iso = true; 3310 dep->endpoint.caps.type_bulk = true; 3311 dep->endpoint.caps.type_int = true; 3312 3313 return dwc3_alloc_trb_pool(dep); 3314 } 3315 3316 #define nostream_work_to_dep(w) (container_of(to_delayed_work(w), struct dwc3_ep, nostream_work)) 3317 static void dwc3_nostream_work(struct work_struct *work) 3318 { 3319 struct dwc3_ep *dep = nostream_work_to_dep(work); 3320 struct dwc3 *dwc = dep->dwc; 3321 unsigned long flags; 3322 3323 spin_lock_irqsave(&dwc->lock, flags); 3324 if (dep->flags & DWC3_EP_STREAM_PRIMED) 3325 goto out; 3326 3327 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) || 3328 (!DWC3_MST_CAPABLE(&dwc->hwparams) && 3329 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))) 3330 goto out; 3331 /* 3332 * If the host rejects a stream due to no active stream, by the 3333 * USB and xHCI spec, the endpoint will be put back to idle 3334 * state. When the host is ready (buffer added/updated), it will 3335 * prime the endpoint to inform the usb device controller. This 3336 * triggers the device controller to issue ERDY to restart the 3337 * stream. However, some hosts don't follow this and keep the 3338 * endpoint in the idle state. No prime will come despite host 3339 * streams are updated, and the device controller will not be 3340 * triggered to generate ERDY to move the next stream data. To 3341 * workaround this and maintain compatibility with various 3342 * hosts, force to reinitiate the stream until the host is ready 3343 * instead of waiting for the host to prime the endpoint. 3344 */ 3345 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) { 3346 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME; 3347 3348 dwc3_send_gadget_generic_command(dwc, cmd, dep->number); 3349 } else { 3350 dep->flags |= DWC3_EP_DELAY_START; 3351 dwc3_stop_active_transfer(dep, true, true); 3352 spin_unlock_irqrestore(&dwc->lock, flags); 3353 return; 3354 } 3355 out: 3356 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; 3357 spin_unlock_irqrestore(&dwc->lock, flags); 3358 } 3359 3360 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) 3361 { 3362 struct dwc3_ep *dep; 3363 bool direction = epnum & 1; 3364 int ret; 3365 u8 num = epnum >> 1; 3366 3367 dep = kzalloc(sizeof(*dep), GFP_KERNEL); 3368 if (!dep) 3369 return -ENOMEM; 3370 3371 dep->dwc = dwc; 3372 dep->number = epnum; 3373 dep->direction = direction; 3374 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); 3375 dwc->eps[epnum] = dep; 3376 dep->combo_num = 0; 3377 dep->start_cmd_status = 0; 3378 3379 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, 3380 direction ? "in" : "out"); 3381 3382 dep->endpoint.name = dep->name; 3383 3384 if (!(dep->number > 1)) { 3385 dep->endpoint.desc = &dwc3_gadget_ep0_desc; 3386 dep->endpoint.comp_desc = NULL; 3387 } 3388 3389 if (num == 0) 3390 ret = dwc3_gadget_init_control_endpoint(dep); 3391 else if (direction) 3392 ret = dwc3_gadget_init_in_endpoint(dep); 3393 else 3394 ret = dwc3_gadget_init_out_endpoint(dep); 3395 3396 if (ret) 3397 return ret; 3398 3399 dep->endpoint.caps.dir_in = direction; 3400 dep->endpoint.caps.dir_out = !direction; 3401 3402 INIT_LIST_HEAD(&dep->pending_list); 3403 INIT_LIST_HEAD(&dep->started_list); 3404 INIT_LIST_HEAD(&dep->cancelled_list); 3405 INIT_DELAYED_WORK(&dep->nostream_work, dwc3_nostream_work); 3406 3407 dwc3_debugfs_create_endpoint_dir(dep); 3408 3409 return 0; 3410 } 3411 3412 static int dwc3_gadget_get_reserved_endpoints(struct dwc3 *dwc, const char *propname, 3413 u8 *eps, u8 num) 3414 { 3415 u8 count; 3416 int ret; 3417 3418 if (!device_property_present(dwc->dev, propname)) 3419 return 0; 3420 3421 ret = device_property_count_u8(dwc->dev, propname); 3422 if (ret < 0) 3423 return ret; 3424 count = ret; 3425 3426 ret = device_property_read_u8_array(dwc->dev, propname, eps, min(num, count)); 3427 if (ret) 3428 return ret; 3429 3430 return count; 3431 } 3432 3433 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) 3434 { 3435 const char *propname = "snps,reserved-endpoints"; 3436 u8 epnum; 3437 u8 reserved_eps[DWC3_ENDPOINTS_NUM]; 3438 u8 count; 3439 u8 num; 3440 int ret; 3441 3442 INIT_LIST_HEAD(&dwc->gadget->ep_list); 3443 3444 ret = dwc3_gadget_get_reserved_endpoints(dwc, propname, 3445 reserved_eps, ARRAY_SIZE(reserved_eps)); 3446 if (ret < 0) { 3447 dev_err(dwc->dev, "failed to read %s\n", propname); 3448 return ret; 3449 } 3450 count = ret; 3451 3452 for (epnum = 0; epnum < total; epnum++) { 3453 for (num = 0; num < count; num++) { 3454 if (epnum == reserved_eps[num]) 3455 break; 3456 } 3457 if (num < count) 3458 continue; 3459 3460 ret = dwc3_gadget_init_endpoint(dwc, epnum); 3461 if (ret) 3462 return ret; 3463 } 3464 3465 return 0; 3466 } 3467 3468 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) 3469 { 3470 struct dwc3_ep *dep; 3471 u8 epnum; 3472 3473 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 3474 dep = dwc->eps[epnum]; 3475 if (!dep) 3476 continue; 3477 /* 3478 * Physical endpoints 0 and 1 are special; they form the 3479 * bi-directional USB endpoint 0. 3480 * 3481 * For those two physical endpoints, we don't allocate a TRB 3482 * pool nor do we add them the endpoints list. Due to that, we 3483 * shouldn't do these two operations otherwise we would end up 3484 * with all sorts of bugs when removing dwc3.ko. 3485 */ 3486 if (epnum != 0 && epnum != 1) { 3487 dwc3_free_trb_pool(dep); 3488 list_del(&dep->endpoint.ep_list); 3489 } 3490 3491 dwc3_debugfs_remove_endpoint_dir(dep); 3492 kfree(dep); 3493 } 3494 } 3495 3496 /* -------------------------------------------------------------------------- */ 3497 3498 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, 3499 struct dwc3_request *req, struct dwc3_trb *trb, 3500 const struct dwc3_event_depevt *event, int status, int chain) 3501 { 3502 unsigned int count; 3503 3504 dwc3_ep_inc_deq(dep); 3505 3506 trace_dwc3_complete_trb(dep, trb); 3507 req->num_trbs--; 3508 3509 /* 3510 * If we're in the middle of series of chained TRBs and we 3511 * receive a short transfer along the way, DWC3 will skip 3512 * through all TRBs including the last TRB in the chain (the 3513 * where CHN bit is zero. DWC3 will also avoid clearing HWO 3514 * bit and SW has to do it manually. 3515 * 3516 * We're going to do that here to avoid problems of HW trying 3517 * to use bogus TRBs for transfers. 3518 */ 3519 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) 3520 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 3521 3522 /* 3523 * For isochronous transfers, the first TRB in a service interval must 3524 * have the Isoc-First type. Track and report its interval frame number. 3525 */ 3526 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 3527 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { 3528 unsigned int frame_number; 3529 3530 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); 3531 frame_number &= ~(dep->interval - 1); 3532 req->request.frame_number = frame_number; 3533 } 3534 3535 /* 3536 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If 3537 * this TRB points to the bounce buffer address, it's a MPS alignment 3538 * TRB. Don't add it to req->remaining calculation. 3539 */ 3540 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) && 3541 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) { 3542 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 3543 return 1; 3544 } 3545 3546 count = trb->size & DWC3_TRB_SIZE_MASK; 3547 req->remaining += count; 3548 3549 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) 3550 return 1; 3551 3552 if (event->status & DEPEVT_STATUS_SHORT && !chain) 3553 return 1; 3554 3555 if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) && 3556 DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC) 3557 return 1; 3558 3559 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) || 3560 (trb->ctrl & DWC3_TRB_CTRL_LST)) 3561 return 1; 3562 3563 return 0; 3564 } 3565 3566 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, 3567 struct dwc3_request *req, const struct dwc3_event_depevt *event, 3568 int status) 3569 { 3570 struct dwc3_trb *trb; 3571 unsigned int num_completed_trbs = req->num_trbs; 3572 unsigned int i; 3573 int ret = 0; 3574 3575 for (i = 0; i < num_completed_trbs; i++) { 3576 trb = &dep->trb_pool[dep->trb_dequeue]; 3577 3578 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, 3579 trb, event, status, 3580 !!(trb->ctrl & DWC3_TRB_CTRL_CHN)); 3581 if (ret) 3582 break; 3583 } 3584 3585 return ret; 3586 } 3587 3588 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) 3589 { 3590 return req->num_pending_sgs == 0 && req->num_trbs == 0; 3591 } 3592 3593 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, 3594 const struct dwc3_event_depevt *event, 3595 struct dwc3_request *req, int status) 3596 { 3597 int request_status; 3598 int ret; 3599 3600 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, status); 3601 3602 req->request.actual = req->request.length - req->remaining; 3603 3604 if (!dwc3_gadget_ep_request_completed(req)) 3605 goto out; 3606 3607 /* 3608 * The event status only reflects the status of the TRB with IOC set. 3609 * For the requests that don't set interrupt on completion, the driver 3610 * needs to check and return the status of the completed TRBs associated 3611 * with the request. Use the status of the last TRB of the request. 3612 */ 3613 if (req->request.no_interrupt) { 3614 struct dwc3_trb *trb; 3615 3616 trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue); 3617 switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) { 3618 case DWC3_TRBSTS_MISSED_ISOC: 3619 /* Isoc endpoint only */ 3620 request_status = -EXDEV; 3621 break; 3622 case DWC3_TRB_STS_XFER_IN_PROG: 3623 /* Applicable when End Transfer with ForceRM=0 */ 3624 case DWC3_TRBSTS_SETUP_PENDING: 3625 /* Control endpoint only */ 3626 case DWC3_TRBSTS_OK: 3627 default: 3628 request_status = 0; 3629 break; 3630 } 3631 } else { 3632 request_status = status; 3633 } 3634 3635 dwc3_gadget_giveback(dep, req, request_status); 3636 3637 out: 3638 return ret; 3639 } 3640 3641 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, 3642 const struct dwc3_event_depevt *event, int status) 3643 { 3644 struct dwc3_request *req; 3645 3646 while (!list_empty(&dep->started_list)) { 3647 int ret; 3648 3649 req = next_request(&dep->started_list); 3650 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, 3651 req, status); 3652 if (ret) 3653 break; 3654 /* 3655 * The endpoint is disabled, let the dwc3_remove_requests() 3656 * handle the cleanup. 3657 */ 3658 if (!dep->endpoint.desc) 3659 break; 3660 } 3661 } 3662 3663 static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep) 3664 { 3665 struct dwc3_request *req; 3666 struct dwc3 *dwc = dep->dwc; 3667 3668 if (!dep->endpoint.desc || !dwc->pullups_connected || 3669 !dwc->connected) 3670 return false; 3671 3672 if (!list_empty(&dep->pending_list)) 3673 return true; 3674 3675 /* 3676 * We only need to check the first entry of the started list. We can 3677 * assume the completed requests are removed from the started list. 3678 */ 3679 req = next_request(&dep->started_list); 3680 if (!req) 3681 return false; 3682 3683 return !dwc3_gadget_ep_request_completed(req); 3684 } 3685 3686 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, 3687 const struct dwc3_event_depevt *event) 3688 { 3689 dep->frame_number = event->parameters; 3690 } 3691 3692 static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep, 3693 const struct dwc3_event_depevt *event, int status) 3694 { 3695 struct dwc3 *dwc = dep->dwc; 3696 bool no_started_trb = true; 3697 3698 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); 3699 3700 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) 3701 goto out; 3702 3703 if (!dep->endpoint.desc) 3704 return no_started_trb; 3705 3706 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 3707 list_empty(&dep->started_list) && 3708 (list_empty(&dep->pending_list) || status == -EXDEV)) 3709 dwc3_stop_active_transfer(dep, true, true); 3710 else if (dwc3_gadget_ep_should_continue(dep)) 3711 if (__dwc3_gadget_kick_transfer(dep) == 0) 3712 no_started_trb = false; 3713 3714 out: 3715 /* 3716 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. 3717 * See dwc3_gadget_linksts_change_interrupt() for 1st half. 3718 */ 3719 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { 3720 u32 reg; 3721 int i; 3722 3723 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { 3724 dep = dwc->eps[i]; 3725 if (!dep) 3726 continue; 3727 3728 if (!(dep->flags & DWC3_EP_ENABLED)) 3729 continue; 3730 3731 if (!list_empty(&dep->started_list)) 3732 return no_started_trb; 3733 } 3734 3735 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 3736 reg |= dwc->u1u2; 3737 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 3738 3739 dwc->u1u2 = 0; 3740 } 3741 3742 return no_started_trb; 3743 } 3744 3745 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, 3746 const struct dwc3_event_depevt *event) 3747 { 3748 int status = 0; 3749 3750 if (!dep->endpoint.desc) 3751 return; 3752 3753 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) 3754 dwc3_gadget_endpoint_frame_from_event(dep, event); 3755 3756 if (event->status & DEPEVT_STATUS_BUSERR) 3757 status = -ECONNRESET; 3758 3759 if (event->status & DEPEVT_STATUS_MISSED_ISOC) 3760 status = -EXDEV; 3761 3762 dwc3_gadget_endpoint_trbs_complete(dep, event, status); 3763 } 3764 3765 static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep, 3766 const struct dwc3_event_depevt *event) 3767 { 3768 int status = 0; 3769 3770 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 3771 3772 if (event->status & DEPEVT_STATUS_BUSERR) 3773 status = -ECONNRESET; 3774 3775 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status)) 3776 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; 3777 } 3778 3779 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, 3780 const struct dwc3_event_depevt *event) 3781 { 3782 dwc3_gadget_endpoint_frame_from_event(dep, event); 3783 3784 /* 3785 * The XferNotReady event is generated only once before the endpoint 3786 * starts. It will be generated again when END_TRANSFER command is 3787 * issued. For some controller versions, the XferNotReady event may be 3788 * generated while the END_TRANSFER command is still in process. Ignore 3789 * it and wait for the next XferNotReady event after the command is 3790 * completed. 3791 */ 3792 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) 3793 return; 3794 3795 (void) __dwc3_gadget_start_isoc(dep); 3796 } 3797 3798 static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep, 3799 const struct dwc3_event_depevt *event) 3800 { 3801 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters); 3802 3803 if (cmd != DWC3_DEPCMD_ENDTRANSFER) 3804 return; 3805 3806 /* 3807 * The END_TRANSFER command will cause the controller to generate a 3808 * NoStream Event, and it's not due to the host DP NoStream rejection. 3809 * Ignore the next NoStream event. 3810 */ 3811 if (dep->stream_capable) 3812 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM; 3813 3814 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; 3815 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 3816 dwc3_gadget_ep_cleanup_cancelled_requests(dep); 3817 3818 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) { 3819 struct dwc3 *dwc = dep->dwc; 3820 3821 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL; 3822 if (dwc3_send_clear_stall_ep_cmd(dep)) { 3823 struct usb_ep *ep0 = &dwc->eps[0]->endpoint; 3824 3825 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name); 3826 if (dwc->delayed_status) 3827 __dwc3_gadget_ep0_set_halt(ep0, 1); 3828 return; 3829 } 3830 3831 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 3832 if (dwc->clear_stall_protocol == dep->number) 3833 dwc3_ep0_send_delayed_status(dwc); 3834 } 3835 3836 if ((dep->flags & DWC3_EP_DELAY_START) && 3837 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) 3838 __dwc3_gadget_kick_transfer(dep); 3839 3840 dep->flags &= ~DWC3_EP_DELAY_START; 3841 } 3842 3843 static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep, 3844 const struct dwc3_event_depevt *event) 3845 { 3846 if (event->status == DEPEVT_STREAMEVT_FOUND) { 3847 cancel_delayed_work(&dep->nostream_work); 3848 dep->flags |= DWC3_EP_STREAM_PRIMED; 3849 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; 3850 return; 3851 } 3852 3853 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */ 3854 switch (event->parameters) { 3855 case DEPEVT_STREAM_PRIME: 3856 cancel_delayed_work(&dep->nostream_work); 3857 dep->flags |= DWC3_EP_STREAM_PRIMED; 3858 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; 3859 break; 3860 case DEPEVT_STREAM_NOSTREAM: 3861 dep->flags &= ~DWC3_EP_STREAM_PRIMED; 3862 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) 3863 queue_delayed_work(system_wq, &dep->nostream_work, 3864 msecs_to_jiffies(100)); 3865 break; 3866 } 3867 } 3868 3869 static void dwc3_endpoint_interrupt(struct dwc3 *dwc, 3870 const struct dwc3_event_depevt *event) 3871 { 3872 struct dwc3_ep *dep; 3873 u8 epnum = event->endpoint_number; 3874 3875 dep = dwc->eps[epnum]; 3876 if (!dep) { 3877 dev_warn(dwc->dev, "spurious event, endpoint %u is not allocated\n", epnum); 3878 return; 3879 } 3880 3881 if (!(dep->flags & DWC3_EP_ENABLED)) { 3882 if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED)) 3883 return; 3884 3885 /* Handle only EPCMDCMPLT when EP disabled */ 3886 if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) && 3887 !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE)) 3888 return; 3889 } 3890 3891 if (epnum == 0 || epnum == 1) { 3892 dwc3_ep0_interrupt(dwc, event); 3893 return; 3894 } 3895 3896 switch (event->endpoint_event) { 3897 case DWC3_DEPEVT_XFERINPROGRESS: 3898 dwc3_gadget_endpoint_transfer_in_progress(dep, event); 3899 break; 3900 case DWC3_DEPEVT_XFERNOTREADY: 3901 dwc3_gadget_endpoint_transfer_not_ready(dep, event); 3902 break; 3903 case DWC3_DEPEVT_EPCMDCMPLT: 3904 dwc3_gadget_endpoint_command_complete(dep, event); 3905 break; 3906 case DWC3_DEPEVT_XFERCOMPLETE: 3907 dwc3_gadget_endpoint_transfer_complete(dep, event); 3908 break; 3909 case DWC3_DEPEVT_STREAMEVT: 3910 dwc3_gadget_endpoint_stream_event(dep, event); 3911 break; 3912 case DWC3_DEPEVT_RXTXFIFOEVT: 3913 break; 3914 default: 3915 dev_err(dwc->dev, "unknown endpoint event %d\n", event->endpoint_event); 3916 break; 3917 } 3918 } 3919 3920 static void dwc3_disconnect_gadget(struct dwc3 *dwc) 3921 { 3922 if (dwc->async_callbacks && dwc->gadget_driver->disconnect) { 3923 spin_unlock(&dwc->lock); 3924 dwc->gadget_driver->disconnect(dwc->gadget); 3925 spin_lock(&dwc->lock); 3926 } 3927 } 3928 3929 static void dwc3_suspend_gadget(struct dwc3 *dwc) 3930 { 3931 if (dwc->async_callbacks && dwc->gadget_driver->suspend) { 3932 spin_unlock(&dwc->lock); 3933 dwc->gadget_driver->suspend(dwc->gadget); 3934 spin_lock(&dwc->lock); 3935 } 3936 } 3937 3938 static void dwc3_resume_gadget(struct dwc3 *dwc) 3939 { 3940 if (dwc->async_callbacks && dwc->gadget_driver->resume) { 3941 spin_unlock(&dwc->lock); 3942 dwc->gadget_driver->resume(dwc->gadget); 3943 spin_lock(&dwc->lock); 3944 } 3945 } 3946 3947 static void dwc3_reset_gadget(struct dwc3 *dwc) 3948 { 3949 if (!dwc->gadget_driver) 3950 return; 3951 3952 if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) { 3953 spin_unlock(&dwc->lock); 3954 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); 3955 spin_lock(&dwc->lock); 3956 } 3957 } 3958 3959 void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, 3960 bool interrupt) 3961 { 3962 struct dwc3 *dwc = dep->dwc; 3963 3964 /* 3965 * Only issue End Transfer command to the control endpoint of a started 3966 * Data Phase. Typically we should only do so in error cases such as 3967 * invalid/unexpected direction as described in the control transfer 3968 * flow of the programming guide. 3969 */ 3970 if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE) 3971 return; 3972 3973 if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP)) 3974 return; 3975 3976 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) || 3977 (dep->flags & DWC3_EP_END_TRANSFER_PENDING)) 3978 return; 3979 3980 /* 3981 * If a Setup packet is received but yet to DMA out, the controller will 3982 * not process the End Transfer command of any endpoint. Polling of its 3983 * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a 3984 * timeout. Delay issuing the End Transfer command until the Setup TRB is 3985 * prepared. 3986 */ 3987 if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) { 3988 dep->flags |= DWC3_EP_DELAY_STOP; 3989 return; 3990 } 3991 3992 /* 3993 * NOTICE: We are violating what the Databook says about the 3994 * EndTransfer command. Ideally we would _always_ wait for the 3995 * EndTransfer Command Completion IRQ, but that's causing too 3996 * much trouble synchronizing between us and gadget driver. 3997 * 3998 * We have discussed this with the IP Provider and it was 3999 * suggested to giveback all requests here. 4000 * 4001 * Note also that a similar handling was tested by Synopsys 4002 * (thanks a lot Paul) and nothing bad has come out of it. 4003 * In short, what we're doing is issuing EndTransfer with 4004 * CMDIOC bit set and delay kicking transfer until the 4005 * EndTransfer command had completed. 4006 * 4007 * As of IP version 3.10a of the DWC_usb3 IP, the controller 4008 * supports a mode to work around the above limitation. The 4009 * software can poll the CMDACT bit in the DEPCMD register 4010 * after issuing a EndTransfer command. This mode is enabled 4011 * by writing GUCTL2[14]. This polling is already done in the 4012 * dwc3_send_gadget_ep_cmd() function so if the mode is 4013 * enabled, the EndTransfer command will have completed upon 4014 * returning from this function. 4015 * 4016 * This mode is NOT available on the DWC_usb31 IP. In this 4017 * case, if the IOC bit is not set, then delay by 1ms 4018 * after issuing the EndTransfer command. This allows for the 4019 * controller to handle the command completely before DWC3 4020 * remove requests attempts to unmap USB request buffers. 4021 */ 4022 4023 __dwc3_stop_active_transfer(dep, force, interrupt); 4024 } 4025 4026 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) 4027 { 4028 u32 epnum; 4029 4030 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 4031 struct dwc3_ep *dep; 4032 int ret; 4033 4034 dep = dwc->eps[epnum]; 4035 if (!dep) 4036 continue; 4037 4038 if (!(dep->flags & DWC3_EP_STALL)) 4039 continue; 4040 4041 dep->flags &= ~DWC3_EP_STALL; 4042 4043 ret = dwc3_send_clear_stall_ep_cmd(dep); 4044 WARN_ON_ONCE(ret); 4045 } 4046 } 4047 4048 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) 4049 { 4050 int reg; 4051 4052 dwc->suspended = false; 4053 4054 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET); 4055 4056 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4057 reg &= ~DWC3_DCTL_INITU1ENA; 4058 reg &= ~DWC3_DCTL_INITU2ENA; 4059 dwc3_gadget_dctl_write_safe(dwc, reg); 4060 4061 dwc->connected = false; 4062 4063 dwc3_disconnect_gadget(dwc); 4064 4065 dwc->gadget->speed = USB_SPEED_UNKNOWN; 4066 dwc->setup_packet_pending = false; 4067 dwc->gadget->wakeup_armed = false; 4068 dwc3_gadget_enable_linksts_evts(dwc, false); 4069 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); 4070 4071 dwc3_ep0_reset_state(dwc); 4072 4073 /* 4074 * Request PM idle to address condition where usage count is 4075 * already decremented to zero, but waiting for the disconnect 4076 * interrupt to set dwc->connected to FALSE. 4077 */ 4078 pm_request_idle(dwc->dev); 4079 } 4080 4081 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) 4082 { 4083 u32 reg; 4084 4085 dwc->suspended = false; 4086 4087 /* 4088 * Ideally, dwc3_reset_gadget() would trigger the function 4089 * drivers to stop any active transfers through ep disable. 4090 * However, for functions which defer ep disable, such as mass 4091 * storage, we will need to rely on the call to stop active 4092 * transfers here, and avoid allowing of request queuing. 4093 */ 4094 dwc->connected = false; 4095 4096 /* 4097 * WORKAROUND: DWC3 revisions <1.88a have an issue which 4098 * would cause a missing Disconnect Event if there's a 4099 * pending Setup Packet in the FIFO. 4100 * 4101 * There's no suggested workaround on the official Bug 4102 * report, which states that "unless the driver/application 4103 * is doing any special handling of a disconnect event, 4104 * there is no functional issue". 4105 * 4106 * Unfortunately, it turns out that we _do_ some special 4107 * handling of a disconnect event, namely complete all 4108 * pending transfers, notify gadget driver of the 4109 * disconnection, and so on. 4110 * 4111 * Our suggested workaround is to follow the Disconnect 4112 * Event steps here, instead, based on a setup_packet_pending 4113 * flag. Such flag gets set whenever we have a SETUP_PENDING 4114 * status for EP0 TRBs and gets cleared on XferComplete for the 4115 * same endpoint. 4116 * 4117 * Refers to: 4118 * 4119 * STAR#9000466709: RTL: Device : Disconnect event not 4120 * generated if setup packet pending in FIFO 4121 */ 4122 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) { 4123 if (dwc->setup_packet_pending) 4124 dwc3_gadget_disconnect_interrupt(dwc); 4125 } 4126 4127 dwc3_reset_gadget(dwc); 4128 4129 /* 4130 * From SNPS databook section 8.1.2, the EP0 should be in setup 4131 * phase. So ensure that EP0 is in setup phase by issuing a stall 4132 * and restart if EP0 is not in setup phase. 4133 */ 4134 dwc3_ep0_reset_state(dwc); 4135 4136 /* 4137 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a 4138 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW 4139 * needs to ensure that it sends "a DEPENDXFER command for any active 4140 * transfers." 4141 */ 4142 dwc3_stop_active_transfers(dwc); 4143 dwc->connected = true; 4144 4145 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4146 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 4147 dwc3_gadget_dctl_write_safe(dwc, reg); 4148 dwc->test_mode = false; 4149 dwc->gadget->wakeup_armed = false; 4150 dwc3_gadget_enable_linksts_evts(dwc, false); 4151 dwc3_clear_stall_all_ep(dwc); 4152 4153 /* Reset device address to zero */ 4154 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 4155 reg &= ~(DWC3_DCFG_DEVADDR_MASK); 4156 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 4157 } 4158 4159 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) 4160 { 4161 struct dwc3_ep *dep; 4162 int ret; 4163 u32 reg; 4164 u8 lanes = 1; 4165 u8 speed; 4166 4167 if (!dwc->softconnect) 4168 return; 4169 4170 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 4171 speed = reg & DWC3_DSTS_CONNECTSPD; 4172 dwc->speed = speed; 4173 4174 if (DWC3_IP_IS(DWC32)) 4175 lanes = DWC3_DSTS_CONNLANES(reg) + 1; 4176 4177 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; 4178 4179 /* 4180 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed 4181 * each time on Connect Done. 4182 * 4183 * Currently we always use the reset value. If any platform 4184 * wants to set this to a different value, we need to add a 4185 * setting and update GCTL.RAMCLKSEL here. 4186 */ 4187 4188 switch (speed) { 4189 case DWC3_DSTS_SUPERSPEED_PLUS: 4190 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 4191 dwc->gadget->ep0->maxpacket = 512; 4192 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; 4193 4194 if (lanes > 1) 4195 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2; 4196 else 4197 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1; 4198 break; 4199 case DWC3_DSTS_SUPERSPEED: 4200 /* 4201 * WORKAROUND: DWC3 revisions <1.90a have an issue which 4202 * would cause a missing USB3 Reset event. 4203 * 4204 * In such situations, we should force a USB3 Reset 4205 * event by calling our dwc3_gadget_reset_interrupt() 4206 * routine. 4207 * 4208 * Refers to: 4209 * 4210 * STAR#9000483510: RTL: SS : USB3 reset event may 4211 * not be generated always when the link enters poll 4212 */ 4213 if (DWC3_VER_IS_PRIOR(DWC3, 190A)) 4214 dwc3_gadget_reset_interrupt(dwc); 4215 4216 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 4217 dwc->gadget->ep0->maxpacket = 512; 4218 dwc->gadget->speed = USB_SPEED_SUPER; 4219 4220 if (lanes > 1) { 4221 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; 4222 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2; 4223 } 4224 break; 4225 case DWC3_DSTS_HIGHSPEED: 4226 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 4227 dwc->gadget->ep0->maxpacket = 64; 4228 dwc->gadget->speed = USB_SPEED_HIGH; 4229 break; 4230 case DWC3_DSTS_FULLSPEED: 4231 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 4232 dwc->gadget->ep0->maxpacket = 64; 4233 dwc->gadget->speed = USB_SPEED_FULL; 4234 break; 4235 } 4236 4237 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; 4238 4239 /* Enable USB2 LPM Capability */ 4240 4241 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) && 4242 !dwc->usb2_gadget_lpm_disable && 4243 (speed != DWC3_DSTS_SUPERSPEED) && 4244 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { 4245 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 4246 reg |= DWC3_DCFG_LPM_CAP; 4247 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 4248 4249 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4250 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); 4251 4252 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold | 4253 (dwc->is_utmi_l1_suspend << 4)); 4254 4255 /* 4256 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and 4257 * DCFG.LPMCap is set, core responses with an ACK and the 4258 * BESL value in the LPM token is less than or equal to LPM 4259 * NYET threshold. 4260 */ 4261 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum, 4262 "LPM Erratum not available on dwc3 revisions < 2.40a\n"); 4263 4264 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) { 4265 reg &= ~DWC3_DCTL_NYET_THRES_MASK; 4266 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold); 4267 } 4268 4269 dwc3_gadget_dctl_write_safe(dwc, reg); 4270 } else { 4271 if (dwc->usb2_gadget_lpm_disable) { 4272 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 4273 reg &= ~DWC3_DCFG_LPM_CAP; 4274 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 4275 } 4276 4277 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4278 reg &= ~DWC3_DCTL_HIRD_THRES_MASK; 4279 dwc3_gadget_dctl_write_safe(dwc, reg); 4280 } 4281 4282 dep = dwc->eps[0]; 4283 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 4284 if (ret) { 4285 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 4286 return; 4287 } 4288 4289 dep = dwc->eps[1]; 4290 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 4291 if (ret) { 4292 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 4293 return; 4294 } 4295 4296 /* 4297 * Configure PHY via GUSB3PIPECTLn if required. 4298 * 4299 * Update GTXFIFOSIZn 4300 * 4301 * In both cases reset values should be sufficient. 4302 */ 4303 } 4304 4305 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, unsigned int evtinfo) 4306 { 4307 dwc->suspended = false; 4308 4309 /* 4310 * TODO take core out of low power mode when that's 4311 * implemented. 4312 */ 4313 4314 if (dwc->async_callbacks && dwc->gadget_driver->resume) { 4315 spin_unlock(&dwc->lock); 4316 dwc->gadget_driver->resume(dwc->gadget); 4317 spin_lock(&dwc->lock); 4318 } 4319 4320 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK; 4321 } 4322 4323 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, 4324 unsigned int evtinfo) 4325 { 4326 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 4327 unsigned int pwropt; 4328 int ret; 4329 int intf_id; 4330 4331 /* 4332 * WORKAROUND: DWC3 < 2.50a have an issue when configured without 4333 * Hibernation mode enabled which would show up when device detects 4334 * host-initiated U3 exit. 4335 * 4336 * In that case, device will generate a Link State Change Interrupt 4337 * from U3 to RESUME which is only necessary if Hibernation is 4338 * configured in. 4339 * 4340 * There are no functional changes due to such spurious event and we 4341 * just need to ignore it. 4342 * 4343 * Refers to: 4344 * 4345 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation 4346 * operational mode 4347 */ 4348 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); 4349 if (DWC3_VER_IS_PRIOR(DWC3, 250A) && 4350 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { 4351 if ((dwc->link_state == DWC3_LINK_STATE_U3) && 4352 (next == DWC3_LINK_STATE_RESUME)) { 4353 return; 4354 } 4355 } 4356 4357 /* 4358 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending 4359 * on the link partner, the USB session might do multiple entry/exit 4360 * of low power states before a transfer takes place. 4361 * 4362 * Due to this problem, we might experience lower throughput. The 4363 * suggested workaround is to disable DCTL[12:9] bits if we're 4364 * transitioning from U1/U2 to U0 and enable those bits again 4365 * after a transfer completes and there are no pending transfers 4366 * on any of the enabled endpoints. 4367 * 4368 * This is the first half of that workaround. 4369 * 4370 * Refers to: 4371 * 4372 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us 4373 * core send LGO_Ux entering U0 4374 */ 4375 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { 4376 if (next == DWC3_LINK_STATE_U0) { 4377 u32 u1u2; 4378 u32 reg; 4379 4380 switch (dwc->link_state) { 4381 case DWC3_LINK_STATE_U1: 4382 case DWC3_LINK_STATE_U2: 4383 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4384 u1u2 = reg & (DWC3_DCTL_INITU2ENA 4385 | DWC3_DCTL_ACCEPTU2ENA 4386 | DWC3_DCTL_INITU1ENA 4387 | DWC3_DCTL_ACCEPTU1ENA); 4388 4389 if (!dwc->u1u2) 4390 dwc->u1u2 = reg & u1u2; 4391 4392 reg &= ~u1u2; 4393 4394 dwc3_gadget_dctl_write_safe(dwc, reg); 4395 break; 4396 default: 4397 /* do nothing */ 4398 break; 4399 } 4400 } 4401 } 4402 4403 switch (next) { 4404 case DWC3_LINK_STATE_U0: 4405 if (dwc->gadget->wakeup_armed || dwc->wakeup_pending_funcs) { 4406 dwc3_gadget_enable_linksts_evts(dwc, false); 4407 dwc3_resume_gadget(dwc); 4408 dwc->suspended = false; 4409 } 4410 break; 4411 case DWC3_LINK_STATE_U1: 4412 if (dwc->speed == USB_SPEED_SUPER) 4413 dwc3_suspend_gadget(dwc); 4414 break; 4415 case DWC3_LINK_STATE_U2: 4416 case DWC3_LINK_STATE_U3: 4417 dwc3_suspend_gadget(dwc); 4418 break; 4419 case DWC3_LINK_STATE_RESUME: 4420 dwc3_resume_gadget(dwc); 4421 break; 4422 default: 4423 /* do nothing */ 4424 break; 4425 } 4426 4427 dwc->link_state = next; 4428 4429 /* Proceed with func wakeup if any interfaces that has requested */ 4430 while (dwc->wakeup_pending_funcs && (next == DWC3_LINK_STATE_U0)) { 4431 intf_id = ffs(dwc->wakeup_pending_funcs) - 1; 4432 ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_DEV_NOTIFICATION, 4433 DWC3_DGCMDPAR_DN_FUNC_WAKE | 4434 DWC3_DGCMDPAR_INTF_SEL(intf_id)); 4435 if (ret) 4436 dev_err(dwc->dev, "Failed to send DN wake for intf %d\n", intf_id); 4437 4438 dwc->wakeup_pending_funcs &= ~BIT(intf_id); 4439 } 4440 } 4441 4442 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, 4443 unsigned int evtinfo) 4444 { 4445 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 4446 4447 if (!dwc->suspended && next == DWC3_LINK_STATE_U3) { 4448 dwc->suspended = true; 4449 dwc3_suspend_gadget(dwc); 4450 } 4451 4452 dwc->link_state = next; 4453 } 4454 4455 static void dwc3_gadget_interrupt(struct dwc3 *dwc, 4456 const struct dwc3_event_devt *event) 4457 { 4458 switch (event->type) { 4459 case DWC3_DEVICE_EVENT_DISCONNECT: 4460 dwc3_gadget_disconnect_interrupt(dwc); 4461 break; 4462 case DWC3_DEVICE_EVENT_RESET: 4463 dwc3_gadget_reset_interrupt(dwc); 4464 break; 4465 case DWC3_DEVICE_EVENT_CONNECT_DONE: 4466 dwc3_gadget_conndone_interrupt(dwc); 4467 break; 4468 case DWC3_DEVICE_EVENT_WAKEUP: 4469 dwc3_gadget_wakeup_interrupt(dwc, event->event_info); 4470 break; 4471 case DWC3_DEVICE_EVENT_HIBER_REQ: 4472 dev_WARN_ONCE(dwc->dev, true, "unexpected hibernation event\n"); 4473 break; 4474 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 4475 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); 4476 break; 4477 case DWC3_DEVICE_EVENT_SUSPEND: 4478 /* It changed to be suspend event for version 2.30a and above */ 4479 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) 4480 dwc3_gadget_suspend_interrupt(dwc, event->event_info); 4481 break; 4482 case DWC3_DEVICE_EVENT_SOF: 4483 case DWC3_DEVICE_EVENT_ERRATIC_ERROR: 4484 case DWC3_DEVICE_EVENT_CMD_CMPL: 4485 case DWC3_DEVICE_EVENT_OVERFLOW: 4486 break; 4487 default: 4488 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); 4489 } 4490 } 4491 4492 static void dwc3_process_event_entry(struct dwc3 *dwc, 4493 const union dwc3_event *event) 4494 { 4495 trace_dwc3_event(event->raw, dwc); 4496 4497 if (!event->type.is_devspec) 4498 dwc3_endpoint_interrupt(dwc, &event->depevt); 4499 else if (event->type.type == DWC3_EVENT_TYPE_DEV) 4500 dwc3_gadget_interrupt(dwc, &event->devt); 4501 else 4502 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); 4503 } 4504 4505 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) 4506 { 4507 struct dwc3 *dwc = evt->dwc; 4508 irqreturn_t ret = IRQ_NONE; 4509 int left; 4510 4511 left = evt->count; 4512 4513 if (!(evt->flags & DWC3_EVENT_PENDING)) 4514 return IRQ_NONE; 4515 4516 while (left > 0) { 4517 union dwc3_event event; 4518 4519 event.raw = *(u32 *) (evt->cache + evt->lpos); 4520 4521 dwc3_process_event_entry(dwc, &event); 4522 4523 /* 4524 * FIXME we wrap around correctly to the next entry as 4525 * almost all entries are 4 bytes in size. There is one 4526 * entry which has 12 bytes which is a regular entry 4527 * followed by 8 bytes data. ATM I don't know how 4528 * things are organized if we get next to the a 4529 * boundary so I worry about that once we try to handle 4530 * that. 4531 */ 4532 evt->lpos = (evt->lpos + 4) % evt->length; 4533 left -= 4; 4534 } 4535 4536 evt->count = 0; 4537 ret = IRQ_HANDLED; 4538 4539 /* Unmask interrupt */ 4540 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), 4541 DWC3_GEVNTSIZ_SIZE(evt->length)); 4542 4543 evt->flags &= ~DWC3_EVENT_PENDING; 4544 /* 4545 * Add an explicit write memory barrier to make sure that the update of 4546 * clearing DWC3_EVENT_PENDING is observed in dwc3_check_event_buf() 4547 */ 4548 wmb(); 4549 4550 if (dwc->imod_interval) { 4551 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 4552 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); 4553 } 4554 4555 return ret; 4556 } 4557 4558 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) 4559 { 4560 struct dwc3_event_buffer *evt = _evt; 4561 struct dwc3 *dwc = evt->dwc; 4562 unsigned long flags; 4563 irqreturn_t ret = IRQ_NONE; 4564 4565 local_bh_disable(); 4566 spin_lock_irqsave(&dwc->lock, flags); 4567 ret = dwc3_process_event_buf(evt); 4568 spin_unlock_irqrestore(&dwc->lock, flags); 4569 local_bh_enable(); 4570 4571 return ret; 4572 } 4573 4574 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) 4575 { 4576 struct dwc3 *dwc = evt->dwc; 4577 u32 amount; 4578 u32 count; 4579 4580 if (pm_runtime_suspended(dwc->dev)) { 4581 dwc->pending_events = true; 4582 /* 4583 * Trigger runtime resume. The get() function will be balanced 4584 * after processing the pending events in dwc3_process_pending 4585 * events(). 4586 */ 4587 pm_runtime_get(dwc->dev); 4588 disable_irq_nosync(dwc->irq_gadget); 4589 return IRQ_HANDLED; 4590 } 4591 4592 /* 4593 * With PCIe legacy interrupt, test shows that top-half irq handler can 4594 * be called again after HW interrupt deassertion. Check if bottom-half 4595 * irq event handler completes before caching new event to prevent 4596 * losing events. 4597 */ 4598 if (evt->flags & DWC3_EVENT_PENDING) 4599 return IRQ_HANDLED; 4600 4601 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); 4602 count &= DWC3_GEVNTCOUNT_MASK; 4603 if (!count) 4604 return IRQ_NONE; 4605 4606 if (count > evt->length) { 4607 dev_err_ratelimited(dwc->dev, "invalid count(%u) > evt->length(%u)\n", 4608 count, evt->length); 4609 return IRQ_NONE; 4610 } 4611 4612 evt->count = count; 4613 evt->flags |= DWC3_EVENT_PENDING; 4614 4615 /* Mask interrupt */ 4616 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), 4617 DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length)); 4618 4619 amount = min(count, evt->length - evt->lpos); 4620 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); 4621 4622 if (amount < count) 4623 memcpy(evt->cache, evt->buf, count - amount); 4624 4625 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); 4626 4627 return IRQ_WAKE_THREAD; 4628 } 4629 4630 static irqreturn_t dwc3_interrupt(int irq, void *_evt) 4631 { 4632 struct dwc3_event_buffer *evt = _evt; 4633 4634 return dwc3_check_event_buf(evt); 4635 } 4636 4637 static int dwc3_gadget_get_irq(struct dwc3 *dwc) 4638 { 4639 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); 4640 int irq; 4641 4642 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral"); 4643 if (irq > 0) 4644 goto out; 4645 4646 if (irq == -EPROBE_DEFER) 4647 goto out; 4648 4649 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3"); 4650 if (irq > 0) 4651 goto out; 4652 4653 if (irq == -EPROBE_DEFER) 4654 goto out; 4655 4656 irq = platform_get_irq(dwc3_pdev, 0); 4657 4658 out: 4659 return irq; 4660 } 4661 4662 static void dwc_gadget_release(struct device *dev) 4663 { 4664 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); 4665 4666 kfree(gadget); 4667 } 4668 4669 /** 4670 * dwc3_gadget_init - initializes gadget related registers 4671 * @dwc: pointer to our controller context structure 4672 * 4673 * Returns 0 on success otherwise negative errno. 4674 */ 4675 int dwc3_gadget_init(struct dwc3 *dwc) 4676 { 4677 int ret; 4678 int irq; 4679 struct device *dev; 4680 4681 irq = dwc3_gadget_get_irq(dwc); 4682 if (irq < 0) { 4683 ret = irq; 4684 goto err0; 4685 } 4686 4687 dwc->irq_gadget = irq; 4688 4689 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, 4690 sizeof(*dwc->ep0_trb) * 2, 4691 &dwc->ep0_trb_addr, GFP_KERNEL); 4692 if (!dwc->ep0_trb) { 4693 dev_err(dwc->dev, "failed to allocate ep0 trb\n"); 4694 ret = -ENOMEM; 4695 goto err0; 4696 } 4697 4698 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); 4699 if (!dwc->setup_buf) { 4700 ret = -ENOMEM; 4701 goto err1; 4702 } 4703 4704 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, 4705 &dwc->bounce_addr, GFP_KERNEL); 4706 if (!dwc->bounce) { 4707 ret = -ENOMEM; 4708 goto err2; 4709 } 4710 4711 init_completion(&dwc->ep0_in_setup); 4712 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL); 4713 if (!dwc->gadget) { 4714 ret = -ENOMEM; 4715 goto err3; 4716 } 4717 4718 4719 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); 4720 dev = &dwc->gadget->dev; 4721 dev->platform_data = dwc; 4722 dwc->gadget->ops = &dwc3_gadget_ops; 4723 dwc->gadget->speed = USB_SPEED_UNKNOWN; 4724 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; 4725 dwc->gadget->sg_supported = true; 4726 dwc->gadget->name = "dwc3-gadget"; 4727 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable; 4728 dwc->gadget->wakeup_capable = true; 4729 4730 /* 4731 * FIXME We might be setting max_speed to <SUPER, however versions 4732 * <2.20a of dwc3 have an issue with metastability (documented 4733 * elsewhere in this driver) which tells us we can't set max speed to 4734 * anything lower than SUPER. 4735 * 4736 * Because gadget.max_speed is only used by composite.c and function 4737 * drivers (i.e. it won't go into dwc3's registers) we are allowing this 4738 * to happen so we avoid sending SuperSpeed Capability descriptor 4739 * together with our BOS descriptor as that could confuse host into 4740 * thinking we can handle super speed. 4741 * 4742 * Note that, in fact, we won't even support GetBOS requests when speed 4743 * is less than super speed because we don't have means, yet, to tell 4744 * composite.c that we are USB 2.0 + LPM ECN. 4745 */ 4746 if (DWC3_VER_IS_PRIOR(DWC3, 220A) && 4747 !dwc->dis_metastability_quirk) 4748 dev_info(dwc->dev, "changing max_speed on rev %08x\n", 4749 dwc->revision); 4750 4751 dwc->gadget->max_speed = dwc->maximum_speed; 4752 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate; 4753 4754 /* 4755 * REVISIT: Here we should clear all pending IRQs to be 4756 * sure we're starting from a well known location. 4757 */ 4758 4759 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); 4760 if (ret) 4761 goto err4; 4762 4763 ret = usb_add_gadget(dwc->gadget); 4764 if (ret) { 4765 dev_err(dwc->dev, "failed to add gadget\n"); 4766 goto err5; 4767 } 4768 4769 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS) 4770 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate); 4771 else 4772 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); 4773 4774 /* No system wakeup if no gadget driver bound */ 4775 if (dwc->sys_wakeup) 4776 device_wakeup_disable(dwc->sysdev); 4777 4778 return 0; 4779 4780 err5: 4781 dwc3_gadget_free_endpoints(dwc); 4782 err4: 4783 usb_put_gadget(dwc->gadget); 4784 dwc->gadget = NULL; 4785 err3: 4786 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 4787 dwc->bounce_addr); 4788 4789 err2: 4790 kfree(dwc->setup_buf); 4791 4792 err1: 4793 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 4794 dwc->ep0_trb, dwc->ep0_trb_addr); 4795 4796 err0: 4797 return ret; 4798 } 4799 4800 /* -------------------------------------------------------------------------- */ 4801 4802 void dwc3_gadget_exit(struct dwc3 *dwc) 4803 { 4804 if (!dwc->gadget) 4805 return; 4806 4807 dwc3_enable_susphy(dwc, false); 4808 usb_del_gadget(dwc->gadget); 4809 dwc3_gadget_free_endpoints(dwc); 4810 usb_put_gadget(dwc->gadget); 4811 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 4812 dwc->bounce_addr); 4813 kfree(dwc->setup_buf); 4814 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 4815 dwc->ep0_trb, dwc->ep0_trb_addr); 4816 } 4817 4818 int dwc3_gadget_suspend(struct dwc3 *dwc) 4819 { 4820 unsigned long flags; 4821 int ret; 4822 4823 ret = dwc3_gadget_soft_disconnect(dwc); 4824 if (ret) 4825 goto err; 4826 4827 spin_lock_irqsave(&dwc->lock, flags); 4828 if (dwc->gadget_driver) 4829 dwc3_disconnect_gadget(dwc); 4830 spin_unlock_irqrestore(&dwc->lock, flags); 4831 4832 return 0; 4833 4834 err: 4835 /* 4836 * Attempt to reset the controller's state. Likely no 4837 * communication can be established until the host 4838 * performs a port reset. 4839 */ 4840 if (dwc->softconnect) 4841 dwc3_gadget_soft_connect(dwc); 4842 4843 return ret; 4844 } 4845 4846 int dwc3_gadget_resume(struct dwc3 *dwc) 4847 { 4848 if (!dwc->gadget_driver || !dwc->softconnect) 4849 return 0; 4850 4851 return dwc3_gadget_soft_connect(dwc); 4852 } 4853