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