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