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