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