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 ret = dwc3_core_soft_reset(dwc); 2750 if (ret) 2751 goto done; 2752 2753 dwc3_event_buffers_setup(dwc); 2754 __dwc3_gadget_start(dwc); 2755 ret = dwc3_gadget_run_stop(dwc, true); 2756 } 2757 2758 done: 2759 pm_runtime_put(dwc->dev); 2760 2761 return ret; 2762 } 2763 2764 static void dwc3_gadget_enable_irq(struct dwc3 *dwc) 2765 { 2766 u32 reg; 2767 2768 /* Enable all but Start and End of Frame IRQs */ 2769 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN | 2770 DWC3_DEVTEN_CMDCMPLTEN | 2771 DWC3_DEVTEN_ERRTICERREN | 2772 DWC3_DEVTEN_WKUPEVTEN | 2773 DWC3_DEVTEN_CONNECTDONEEN | 2774 DWC3_DEVTEN_USBRSTEN | 2775 DWC3_DEVTEN_DISCONNEVTEN); 2776 2777 if (DWC3_VER_IS_PRIOR(DWC3, 250A)) 2778 reg |= DWC3_DEVTEN_ULSTCNGEN; 2779 2780 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */ 2781 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) 2782 reg |= DWC3_DEVTEN_U3L2L1SUSPEN; 2783 2784 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); 2785 } 2786 2787 static void dwc3_gadget_disable_irq(struct dwc3 *dwc) 2788 { 2789 /* mask all interrupts */ 2790 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); 2791 } 2792 2793 static irqreturn_t dwc3_interrupt(int irq, void *_dwc); 2794 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); 2795 2796 /** 2797 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG 2798 * @dwc: pointer to our context structure 2799 * 2800 * The following looks like complex but it's actually very simple. In order to 2801 * calculate the number of packets we can burst at once on OUT transfers, we're 2802 * gonna use RxFIFO size. 2803 * 2804 * To calculate RxFIFO size we need two numbers: 2805 * MDWIDTH = size, in bits, of the internal memory bus 2806 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) 2807 * 2808 * Given these two numbers, the formula is simple: 2809 * 2810 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; 2811 * 2812 * 24 bytes is for 3x SETUP packets 2813 * 16 bytes is a clock domain crossing tolerance 2814 * 2815 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; 2816 */ 2817 static void dwc3_gadget_setup_nump(struct dwc3 *dwc) 2818 { 2819 u32 ram2_depth; 2820 u32 mdwidth; 2821 u32 nump; 2822 u32 reg; 2823 2824 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); 2825 mdwidth = dwc3_mdwidth(dwc); 2826 2827 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; 2828 nump = min_t(u32, nump, 16); 2829 2830 /* update NumP */ 2831 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2832 reg &= ~DWC3_DCFG_NUMP_MASK; 2833 reg |= nump << DWC3_DCFG_NUMP_SHIFT; 2834 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2835 } 2836 2837 static int __dwc3_gadget_start(struct dwc3 *dwc) 2838 { 2839 struct dwc3_ep *dep; 2840 int ret = 0; 2841 u32 reg; 2842 2843 /* 2844 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if 2845 * the core supports IMOD, disable it. 2846 */ 2847 if (dwc->imod_interval) { 2848 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); 2849 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 2850 } else if (dwc3_has_imod(dwc)) { 2851 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); 2852 } 2853 2854 /* 2855 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP 2856 * field instead of letting dwc3 itself calculate that automatically. 2857 * 2858 * This way, we maximize the chances that we'll be able to get several 2859 * bursts of data without going through any sort of endpoint throttling. 2860 */ 2861 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); 2862 if (DWC3_IP_IS(DWC3)) 2863 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; 2864 else 2865 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; 2866 2867 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); 2868 2869 dwc3_gadget_setup_nump(dwc); 2870 2871 /* 2872 * Currently the controller handles single stream only. So, Ignore 2873 * Packet Pending bit for stream selection and don't search for another 2874 * stream if the host sends Data Packet with PP=0 (for OUT direction) or 2875 * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves 2876 * the stream performance. 2877 */ 2878 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2879 reg |= DWC3_DCFG_IGNSTRMPP; 2880 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2881 2882 /* Enable MST by default if the device is capable of MST */ 2883 if (DWC3_MST_CAPABLE(&dwc->hwparams)) { 2884 reg = dwc3_readl(dwc->regs, DWC3_DCFG1); 2885 reg &= ~DWC3_DCFG1_DIS_MST_ENH; 2886 dwc3_writel(dwc->regs, DWC3_DCFG1, reg); 2887 } 2888 2889 /* Start with SuperSpeed Default */ 2890 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 2891 2892 dep = dwc->eps[0]; 2893 dep->flags = 0; 2894 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 2895 if (ret) { 2896 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2897 goto err0; 2898 } 2899 2900 dep = dwc->eps[1]; 2901 dep->flags = 0; 2902 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 2903 if (ret) { 2904 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2905 goto err1; 2906 } 2907 2908 /* begin to receive SETUP packets */ 2909 dwc->ep0state = EP0_SETUP_PHASE; 2910 dwc->ep0_bounced = false; 2911 dwc->link_state = DWC3_LINK_STATE_SS_DIS; 2912 dwc->delayed_status = false; 2913 dwc3_ep0_out_start(dwc); 2914 2915 dwc3_gadget_enable_irq(dwc); 2916 2917 return 0; 2918 2919 err1: 2920 __dwc3_gadget_ep_disable(dwc->eps[0]); 2921 2922 err0: 2923 return ret; 2924 } 2925 2926 static int dwc3_gadget_start(struct usb_gadget *g, 2927 struct usb_gadget_driver *driver) 2928 { 2929 struct dwc3 *dwc = gadget_to_dwc(g); 2930 unsigned long flags; 2931 int ret; 2932 int irq; 2933 2934 irq = dwc->irq_gadget; 2935 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, 2936 IRQF_SHARED, "dwc3", dwc->ev_buf); 2937 if (ret) { 2938 dev_err(dwc->dev, "failed to request irq #%d --> %d\n", 2939 irq, ret); 2940 return ret; 2941 } 2942 2943 spin_lock_irqsave(&dwc->lock, flags); 2944 dwc->gadget_driver = driver; 2945 spin_unlock_irqrestore(&dwc->lock, flags); 2946 2947 return 0; 2948 } 2949 2950 static void __dwc3_gadget_stop(struct dwc3 *dwc) 2951 { 2952 dwc3_gadget_disable_irq(dwc); 2953 __dwc3_gadget_ep_disable(dwc->eps[0]); 2954 __dwc3_gadget_ep_disable(dwc->eps[1]); 2955 } 2956 2957 static int dwc3_gadget_stop(struct usb_gadget *g) 2958 { 2959 struct dwc3 *dwc = gadget_to_dwc(g); 2960 unsigned long flags; 2961 2962 spin_lock_irqsave(&dwc->lock, flags); 2963 dwc->gadget_driver = NULL; 2964 dwc->max_cfg_eps = 0; 2965 spin_unlock_irqrestore(&dwc->lock, flags); 2966 2967 free_irq(dwc->irq_gadget, dwc->ev_buf); 2968 2969 return 0; 2970 } 2971 2972 static void dwc3_gadget_config_params(struct usb_gadget *g, 2973 struct usb_dcd_config_params *params) 2974 { 2975 struct dwc3 *dwc = gadget_to_dwc(g); 2976 2977 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED; 2978 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED; 2979 2980 /* Recommended BESL */ 2981 if (!dwc->dis_enblslpm_quirk) { 2982 /* 2983 * If the recommended BESL baseline is 0 or if the BESL deep is 2984 * less than 2, Microsoft's Windows 10 host usb stack will issue 2985 * a usb reset immediately after it receives the extended BOS 2986 * descriptor and the enumeration will fail. To maintain 2987 * compatibility with the Windows' usb stack, let's set the 2988 * recommended BESL baseline to 1 and clamp the BESL deep to be 2989 * within 2 to 15. 2990 */ 2991 params->besl_baseline = 1; 2992 if (dwc->is_utmi_l1_suspend) 2993 params->besl_deep = 2994 clamp_t(u8, dwc->hird_threshold, 2, 15); 2995 } 2996 2997 /* U1 Device exit Latency */ 2998 if (dwc->dis_u1_entry_quirk) 2999 params->bU1devExitLat = 0; 3000 else 3001 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT; 3002 3003 /* U2 Device exit Latency */ 3004 if (dwc->dis_u2_entry_quirk) 3005 params->bU2DevExitLat = 0; 3006 else 3007 params->bU2DevExitLat = 3008 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT); 3009 } 3010 3011 static void dwc3_gadget_set_speed(struct usb_gadget *g, 3012 enum usb_device_speed speed) 3013 { 3014 struct dwc3 *dwc = gadget_to_dwc(g); 3015 unsigned long flags; 3016 3017 spin_lock_irqsave(&dwc->lock, flags); 3018 dwc->gadget_max_speed = speed; 3019 spin_unlock_irqrestore(&dwc->lock, flags); 3020 } 3021 3022 static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g, 3023 enum usb_ssp_rate rate) 3024 { 3025 struct dwc3 *dwc = gadget_to_dwc(g); 3026 unsigned long flags; 3027 3028 spin_lock_irqsave(&dwc->lock, flags); 3029 dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS; 3030 dwc->gadget_ssp_rate = rate; 3031 spin_unlock_irqrestore(&dwc->lock, flags); 3032 } 3033 3034 static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA) 3035 { 3036 struct dwc3 *dwc = gadget_to_dwc(g); 3037 union power_supply_propval val = {0}; 3038 int ret; 3039 3040 if (dwc->usb2_phy) 3041 return usb_phy_set_power(dwc->usb2_phy, mA); 3042 3043 if (!dwc->usb_psy) 3044 return -EOPNOTSUPP; 3045 3046 val.intval = 1000 * mA; 3047 ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val); 3048 3049 return ret; 3050 } 3051 3052 /** 3053 * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration 3054 * @g: pointer to the USB gadget 3055 * 3056 * Used to record the maximum number of endpoints being used in a USB composite 3057 * device. (across all configurations) This is to be used in the calculation 3058 * of the TXFIFO sizes when resizing internal memory for individual endpoints. 3059 * It will help ensured that the resizing logic reserves enough space for at 3060 * least one max packet. 3061 */ 3062 static int dwc3_gadget_check_config(struct usb_gadget *g) 3063 { 3064 struct dwc3 *dwc = gadget_to_dwc(g); 3065 struct usb_ep *ep; 3066 int fifo_size = 0; 3067 int ram1_depth; 3068 int ep_num = 0; 3069 3070 if (!dwc->do_fifo_resize) 3071 return 0; 3072 3073 list_for_each_entry(ep, &g->ep_list, ep_list) { 3074 /* Only interested in the IN endpoints */ 3075 if (ep->claimed && (ep->address & USB_DIR_IN)) 3076 ep_num++; 3077 } 3078 3079 if (ep_num <= dwc->max_cfg_eps) 3080 return 0; 3081 3082 /* Update the max number of eps in the composition */ 3083 dwc->max_cfg_eps = ep_num; 3084 3085 fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps); 3086 /* Based on the equation, increment by one for every ep */ 3087 fifo_size += dwc->max_cfg_eps; 3088 3089 /* Check if we can fit a single fifo per endpoint */ 3090 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); 3091 if (fifo_size > ram1_depth) 3092 return -ENOMEM; 3093 3094 return 0; 3095 } 3096 3097 static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable) 3098 { 3099 struct dwc3 *dwc = gadget_to_dwc(g); 3100 unsigned long flags; 3101 3102 spin_lock_irqsave(&dwc->lock, flags); 3103 dwc->async_callbacks = enable; 3104 spin_unlock_irqrestore(&dwc->lock, flags); 3105 } 3106 3107 static const struct usb_gadget_ops dwc3_gadget_ops = { 3108 .get_frame = dwc3_gadget_get_frame, 3109 .wakeup = dwc3_gadget_wakeup, 3110 .func_wakeup = dwc3_gadget_func_wakeup, 3111 .set_remote_wakeup = dwc3_gadget_set_remote_wakeup, 3112 .set_selfpowered = dwc3_gadget_set_selfpowered, 3113 .pullup = dwc3_gadget_pullup, 3114 .udc_start = dwc3_gadget_start, 3115 .udc_stop = dwc3_gadget_stop, 3116 .udc_set_speed = dwc3_gadget_set_speed, 3117 .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate, 3118 .get_config_params = dwc3_gadget_config_params, 3119 .vbus_draw = dwc3_gadget_vbus_draw, 3120 .check_config = dwc3_gadget_check_config, 3121 .udc_async_callbacks = dwc3_gadget_async_callbacks, 3122 }; 3123 3124 /* -------------------------------------------------------------------------- */ 3125 3126 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) 3127 { 3128 struct dwc3 *dwc = dep->dwc; 3129 3130 usb_ep_set_maxpacket_limit(&dep->endpoint, 512); 3131 dep->endpoint.maxburst = 1; 3132 dep->endpoint.ops = &dwc3_gadget_ep0_ops; 3133 if (!dep->direction) 3134 dwc->gadget->ep0 = &dep->endpoint; 3135 3136 dep->endpoint.caps.type_control = true; 3137 3138 return 0; 3139 } 3140 3141 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) 3142 { 3143 struct dwc3 *dwc = dep->dwc; 3144 u32 mdwidth; 3145 int size; 3146 int maxpacket; 3147 3148 mdwidth = dwc3_mdwidth(dwc); 3149 3150 /* MDWIDTH is represented in bits, we need it in bytes */ 3151 mdwidth /= 8; 3152 3153 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); 3154 if (DWC3_IP_IS(DWC3)) 3155 size = DWC3_GTXFIFOSIZ_TXFDEP(size); 3156 else 3157 size = DWC31_GTXFIFOSIZ_TXFDEP(size); 3158 3159 /* 3160 * maxpacket size is determined as part of the following, after assuming 3161 * a mult value of one maxpacket: 3162 * DWC3 revision 280A and prior: 3163 * fifo_size = mult * (max_packet / mdwidth) + 1; 3164 * maxpacket = mdwidth * (fifo_size - 1); 3165 * 3166 * DWC3 revision 290A and onwards: 3167 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 3168 * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth; 3169 */ 3170 if (DWC3_VER_IS_PRIOR(DWC3, 290A)) 3171 maxpacket = mdwidth * (size - 1); 3172 else 3173 maxpacket = mdwidth * ((size - 1) - 1) - mdwidth; 3174 3175 /* Functionally, space for one max packet is sufficient */ 3176 size = min_t(int, maxpacket, 1024); 3177 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 3178 3179 dep->endpoint.max_streams = 16; 3180 dep->endpoint.ops = &dwc3_gadget_ep_ops; 3181 list_add_tail(&dep->endpoint.ep_list, 3182 &dwc->gadget->ep_list); 3183 dep->endpoint.caps.type_iso = true; 3184 dep->endpoint.caps.type_bulk = true; 3185 dep->endpoint.caps.type_int = true; 3186 3187 return dwc3_alloc_trb_pool(dep); 3188 } 3189 3190 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) 3191 { 3192 struct dwc3 *dwc = dep->dwc; 3193 u32 mdwidth; 3194 int size; 3195 3196 mdwidth = dwc3_mdwidth(dwc); 3197 3198 /* MDWIDTH is represented in bits, convert to bytes */ 3199 mdwidth /= 8; 3200 3201 /* All OUT endpoints share a single RxFIFO space */ 3202 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0)); 3203 if (DWC3_IP_IS(DWC3)) 3204 size = DWC3_GRXFIFOSIZ_RXFDEP(size); 3205 else 3206 size = DWC31_GRXFIFOSIZ_RXFDEP(size); 3207 3208 /* FIFO depth is in MDWDITH bytes */ 3209 size *= mdwidth; 3210 3211 /* 3212 * To meet performance requirement, a minimum recommended RxFIFO size 3213 * is defined as follow: 3214 * RxFIFO size >= (3 x MaxPacketSize) + 3215 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin) 3216 * 3217 * Then calculate the max packet limit as below. 3218 */ 3219 size -= (3 * 8) + 16; 3220 if (size < 0) 3221 size = 0; 3222 else 3223 size /= 3; 3224 3225 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 3226 dep->endpoint.max_streams = 16; 3227 dep->endpoint.ops = &dwc3_gadget_ep_ops; 3228 list_add_tail(&dep->endpoint.ep_list, 3229 &dwc->gadget->ep_list); 3230 dep->endpoint.caps.type_iso = true; 3231 dep->endpoint.caps.type_bulk = true; 3232 dep->endpoint.caps.type_int = true; 3233 3234 return dwc3_alloc_trb_pool(dep); 3235 } 3236 3237 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) 3238 { 3239 struct dwc3_ep *dep; 3240 bool direction = epnum & 1; 3241 int ret; 3242 u8 num = epnum >> 1; 3243 3244 dep = kzalloc(sizeof(*dep), GFP_KERNEL); 3245 if (!dep) 3246 return -ENOMEM; 3247 3248 dep->dwc = dwc; 3249 dep->number = epnum; 3250 dep->direction = direction; 3251 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); 3252 dwc->eps[epnum] = dep; 3253 dep->combo_num = 0; 3254 dep->start_cmd_status = 0; 3255 3256 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, 3257 direction ? "in" : "out"); 3258 3259 dep->endpoint.name = dep->name; 3260 3261 if (!(dep->number > 1)) { 3262 dep->endpoint.desc = &dwc3_gadget_ep0_desc; 3263 dep->endpoint.comp_desc = NULL; 3264 } 3265 3266 if (num == 0) 3267 ret = dwc3_gadget_init_control_endpoint(dep); 3268 else if (direction) 3269 ret = dwc3_gadget_init_in_endpoint(dep); 3270 else 3271 ret = dwc3_gadget_init_out_endpoint(dep); 3272 3273 if (ret) 3274 return ret; 3275 3276 dep->endpoint.caps.dir_in = direction; 3277 dep->endpoint.caps.dir_out = !direction; 3278 3279 INIT_LIST_HEAD(&dep->pending_list); 3280 INIT_LIST_HEAD(&dep->started_list); 3281 INIT_LIST_HEAD(&dep->cancelled_list); 3282 3283 dwc3_debugfs_create_endpoint_dir(dep); 3284 3285 return 0; 3286 } 3287 3288 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) 3289 { 3290 u8 epnum; 3291 3292 INIT_LIST_HEAD(&dwc->gadget->ep_list); 3293 3294 for (epnum = 0; epnum < total; epnum++) { 3295 int ret; 3296 3297 ret = dwc3_gadget_init_endpoint(dwc, epnum); 3298 if (ret) 3299 return ret; 3300 } 3301 3302 return 0; 3303 } 3304 3305 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) 3306 { 3307 struct dwc3_ep *dep; 3308 u8 epnum; 3309 3310 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 3311 dep = dwc->eps[epnum]; 3312 if (!dep) 3313 continue; 3314 /* 3315 * Physical endpoints 0 and 1 are special; they form the 3316 * bi-directional USB endpoint 0. 3317 * 3318 * For those two physical endpoints, we don't allocate a TRB 3319 * pool nor do we add them the endpoints list. Due to that, we 3320 * shouldn't do these two operations otherwise we would end up 3321 * with all sorts of bugs when removing dwc3.ko. 3322 */ 3323 if (epnum != 0 && epnum != 1) { 3324 dwc3_free_trb_pool(dep); 3325 list_del(&dep->endpoint.ep_list); 3326 } 3327 3328 dwc3_debugfs_remove_endpoint_dir(dep); 3329 kfree(dep); 3330 } 3331 } 3332 3333 /* -------------------------------------------------------------------------- */ 3334 3335 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, 3336 struct dwc3_request *req, struct dwc3_trb *trb, 3337 const struct dwc3_event_depevt *event, int status, int chain) 3338 { 3339 unsigned int count; 3340 3341 dwc3_ep_inc_deq(dep); 3342 3343 trace_dwc3_complete_trb(dep, trb); 3344 req->num_trbs--; 3345 3346 /* 3347 * If we're in the middle of series of chained TRBs and we 3348 * receive a short transfer along the way, DWC3 will skip 3349 * through all TRBs including the last TRB in the chain (the 3350 * where CHN bit is zero. DWC3 will also avoid clearing HWO 3351 * bit and SW has to do it manually. 3352 * 3353 * We're going to do that here to avoid problems of HW trying 3354 * to use bogus TRBs for transfers. 3355 */ 3356 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) 3357 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 3358 3359 /* 3360 * For isochronous transfers, the first TRB in a service interval must 3361 * have the Isoc-First type. Track and report its interval frame number. 3362 */ 3363 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 3364 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { 3365 unsigned int frame_number; 3366 3367 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); 3368 frame_number &= ~(dep->interval - 1); 3369 req->request.frame_number = frame_number; 3370 } 3371 3372 /* 3373 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If 3374 * this TRB points to the bounce buffer address, it's a MPS alignment 3375 * TRB. Don't add it to req->remaining calculation. 3376 */ 3377 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) && 3378 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) { 3379 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 3380 return 1; 3381 } 3382 3383 count = trb->size & DWC3_TRB_SIZE_MASK; 3384 req->remaining += count; 3385 3386 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) 3387 return 1; 3388 3389 if (event->status & DEPEVT_STATUS_SHORT && !chain) 3390 return 1; 3391 3392 if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) && 3393 DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC) 3394 return 1; 3395 3396 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) || 3397 (trb->ctrl & DWC3_TRB_CTRL_LST)) 3398 return 1; 3399 3400 return 0; 3401 } 3402 3403 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, 3404 struct dwc3_request *req, const struct dwc3_event_depevt *event, 3405 int status) 3406 { 3407 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; 3408 struct scatterlist *sg = req->sg; 3409 struct scatterlist *s; 3410 unsigned int num_queued = req->num_queued_sgs; 3411 unsigned int i; 3412 int ret = 0; 3413 3414 for_each_sg(sg, s, num_queued, i) { 3415 trb = &dep->trb_pool[dep->trb_dequeue]; 3416 3417 req->sg = sg_next(s); 3418 req->num_queued_sgs--; 3419 3420 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, 3421 trb, event, status, true); 3422 if (ret) 3423 break; 3424 } 3425 3426 return ret; 3427 } 3428 3429 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep, 3430 struct dwc3_request *req, const struct dwc3_event_depevt *event, 3431 int status) 3432 { 3433 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; 3434 3435 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb, 3436 event, status, false); 3437 } 3438 3439 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) 3440 { 3441 return req->num_pending_sgs == 0 && req->num_queued_sgs == 0; 3442 } 3443 3444 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, 3445 const struct dwc3_event_depevt *event, 3446 struct dwc3_request *req, int status) 3447 { 3448 int request_status; 3449 int ret; 3450 3451 if (req->request.num_mapped_sgs) 3452 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, 3453 status); 3454 else 3455 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, 3456 status); 3457 3458 req->request.actual = req->request.length - req->remaining; 3459 3460 if (!dwc3_gadget_ep_request_completed(req)) 3461 goto out; 3462 3463 if (req->needs_extra_trb) { 3464 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, 3465 status); 3466 req->needs_extra_trb = false; 3467 } 3468 3469 /* 3470 * The event status only reflects the status of the TRB with IOC set. 3471 * For the requests that don't set interrupt on completion, the driver 3472 * needs to check and return the status of the completed TRBs associated 3473 * with the request. Use the status of the last TRB of the request. 3474 */ 3475 if (req->request.no_interrupt) { 3476 struct dwc3_trb *trb; 3477 3478 trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue); 3479 switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) { 3480 case DWC3_TRBSTS_MISSED_ISOC: 3481 /* Isoc endpoint only */ 3482 request_status = -EXDEV; 3483 break; 3484 case DWC3_TRB_STS_XFER_IN_PROG: 3485 /* Applicable when End Transfer with ForceRM=0 */ 3486 case DWC3_TRBSTS_SETUP_PENDING: 3487 /* Control endpoint only */ 3488 case DWC3_TRBSTS_OK: 3489 default: 3490 request_status = 0; 3491 break; 3492 } 3493 } else { 3494 request_status = status; 3495 } 3496 3497 dwc3_gadget_giveback(dep, req, request_status); 3498 3499 out: 3500 return ret; 3501 } 3502 3503 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, 3504 const struct dwc3_event_depevt *event, int status) 3505 { 3506 struct dwc3_request *req; 3507 3508 while (!list_empty(&dep->started_list)) { 3509 int ret; 3510 3511 req = next_request(&dep->started_list); 3512 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, 3513 req, status); 3514 if (ret) 3515 break; 3516 /* 3517 * The endpoint is disabled, let the dwc3_remove_requests() 3518 * handle the cleanup. 3519 */ 3520 if (!dep->endpoint.desc) 3521 break; 3522 } 3523 } 3524 3525 static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep) 3526 { 3527 struct dwc3_request *req; 3528 struct dwc3 *dwc = dep->dwc; 3529 3530 if (!dep->endpoint.desc || !dwc->pullups_connected || 3531 !dwc->connected) 3532 return false; 3533 3534 if (!list_empty(&dep->pending_list)) 3535 return true; 3536 3537 /* 3538 * We only need to check the first entry of the started list. We can 3539 * assume the completed requests are removed from the started list. 3540 */ 3541 req = next_request(&dep->started_list); 3542 if (!req) 3543 return false; 3544 3545 return !dwc3_gadget_ep_request_completed(req); 3546 } 3547 3548 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, 3549 const struct dwc3_event_depevt *event) 3550 { 3551 dep->frame_number = event->parameters; 3552 } 3553 3554 static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep, 3555 const struct dwc3_event_depevt *event, int status) 3556 { 3557 struct dwc3 *dwc = dep->dwc; 3558 bool no_started_trb = true; 3559 3560 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); 3561 3562 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) 3563 goto out; 3564 3565 if (!dep->endpoint.desc) 3566 return no_started_trb; 3567 3568 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 3569 list_empty(&dep->started_list) && 3570 (list_empty(&dep->pending_list) || status == -EXDEV)) 3571 dwc3_stop_active_transfer(dep, true, true); 3572 else if (dwc3_gadget_ep_should_continue(dep)) 3573 if (__dwc3_gadget_kick_transfer(dep) == 0) 3574 no_started_trb = false; 3575 3576 out: 3577 /* 3578 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. 3579 * See dwc3_gadget_linksts_change_interrupt() for 1st half. 3580 */ 3581 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { 3582 u32 reg; 3583 int i; 3584 3585 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { 3586 dep = dwc->eps[i]; 3587 3588 if (!(dep->flags & DWC3_EP_ENABLED)) 3589 continue; 3590 3591 if (!list_empty(&dep->started_list)) 3592 return no_started_trb; 3593 } 3594 3595 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 3596 reg |= dwc->u1u2; 3597 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 3598 3599 dwc->u1u2 = 0; 3600 } 3601 3602 return no_started_trb; 3603 } 3604 3605 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, 3606 const struct dwc3_event_depevt *event) 3607 { 3608 int status = 0; 3609 3610 if (!dep->endpoint.desc) 3611 return; 3612 3613 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) 3614 dwc3_gadget_endpoint_frame_from_event(dep, event); 3615 3616 if (event->status & DEPEVT_STATUS_BUSERR) 3617 status = -ECONNRESET; 3618 3619 if (event->status & DEPEVT_STATUS_MISSED_ISOC) 3620 status = -EXDEV; 3621 3622 dwc3_gadget_endpoint_trbs_complete(dep, event, status); 3623 } 3624 3625 static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep, 3626 const struct dwc3_event_depevt *event) 3627 { 3628 int status = 0; 3629 3630 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 3631 3632 if (event->status & DEPEVT_STATUS_BUSERR) 3633 status = -ECONNRESET; 3634 3635 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status)) 3636 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; 3637 } 3638 3639 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, 3640 const struct dwc3_event_depevt *event) 3641 { 3642 dwc3_gadget_endpoint_frame_from_event(dep, event); 3643 3644 /* 3645 * The XferNotReady event is generated only once before the endpoint 3646 * starts. It will be generated again when END_TRANSFER command is 3647 * issued. For some controller versions, the XferNotReady event may be 3648 * generated while the END_TRANSFER command is still in process. Ignore 3649 * it and wait for the next XferNotReady event after the command is 3650 * completed. 3651 */ 3652 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) 3653 return; 3654 3655 (void) __dwc3_gadget_start_isoc(dep); 3656 } 3657 3658 static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep, 3659 const struct dwc3_event_depevt *event) 3660 { 3661 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters); 3662 3663 if (cmd != DWC3_DEPCMD_ENDTRANSFER) 3664 return; 3665 3666 /* 3667 * The END_TRANSFER command will cause the controller to generate a 3668 * NoStream Event, and it's not due to the host DP NoStream rejection. 3669 * Ignore the next NoStream event. 3670 */ 3671 if (dep->stream_capable) 3672 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM; 3673 3674 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; 3675 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 3676 dwc3_gadget_ep_cleanup_cancelled_requests(dep); 3677 3678 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) { 3679 struct dwc3 *dwc = dep->dwc; 3680 3681 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL; 3682 if (dwc3_send_clear_stall_ep_cmd(dep)) { 3683 struct usb_ep *ep0 = &dwc->eps[0]->endpoint; 3684 3685 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name); 3686 if (dwc->delayed_status) 3687 __dwc3_gadget_ep0_set_halt(ep0, 1); 3688 return; 3689 } 3690 3691 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 3692 if (dwc->clear_stall_protocol == dep->number) 3693 dwc3_ep0_send_delayed_status(dwc); 3694 } 3695 3696 if ((dep->flags & DWC3_EP_DELAY_START) && 3697 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) 3698 __dwc3_gadget_kick_transfer(dep); 3699 3700 dep->flags &= ~DWC3_EP_DELAY_START; 3701 } 3702 3703 static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep, 3704 const struct dwc3_event_depevt *event) 3705 { 3706 struct dwc3 *dwc = dep->dwc; 3707 3708 if (event->status == DEPEVT_STREAMEVT_FOUND) { 3709 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; 3710 goto out; 3711 } 3712 3713 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */ 3714 switch (event->parameters) { 3715 case DEPEVT_STREAM_PRIME: 3716 /* 3717 * If the host can properly transition the endpoint state from 3718 * idle to prime after a NoStream rejection, there's no need to 3719 * force restarting the endpoint to reinitiate the stream. To 3720 * simplify the check, assume the host follows the USB spec if 3721 * it primed the endpoint more than once. 3722 */ 3723 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) { 3724 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED) 3725 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM; 3726 else 3727 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; 3728 } 3729 3730 break; 3731 case DEPEVT_STREAM_NOSTREAM: 3732 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) || 3733 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) || 3734 (!DWC3_MST_CAPABLE(&dwc->hwparams) && 3735 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))) 3736 break; 3737 3738 /* 3739 * If the host rejects a stream due to no active stream, by the 3740 * USB and xHCI spec, the endpoint will be put back to idle 3741 * state. When the host is ready (buffer added/updated), it will 3742 * prime the endpoint to inform the usb device controller. This 3743 * triggers the device controller to issue ERDY to restart the 3744 * stream. However, some hosts don't follow this and keep the 3745 * endpoint in the idle state. No prime will come despite host 3746 * streams are updated, and the device controller will not be 3747 * triggered to generate ERDY to move the next stream data. To 3748 * workaround this and maintain compatibility with various 3749 * hosts, force to reinitiate the stream until the host is ready 3750 * instead of waiting for the host to prime the endpoint. 3751 */ 3752 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) { 3753 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME; 3754 3755 dwc3_send_gadget_generic_command(dwc, cmd, dep->number); 3756 } else { 3757 dep->flags |= DWC3_EP_DELAY_START; 3758 dwc3_stop_active_transfer(dep, true, true); 3759 return; 3760 } 3761 break; 3762 } 3763 3764 out: 3765 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; 3766 } 3767 3768 static void dwc3_endpoint_interrupt(struct dwc3 *dwc, 3769 const struct dwc3_event_depevt *event) 3770 { 3771 struct dwc3_ep *dep; 3772 u8 epnum = event->endpoint_number; 3773 3774 dep = dwc->eps[epnum]; 3775 3776 if (!(dep->flags & DWC3_EP_ENABLED)) { 3777 if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED)) 3778 return; 3779 3780 /* Handle only EPCMDCMPLT when EP disabled */ 3781 if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) && 3782 !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE)) 3783 return; 3784 } 3785 3786 if (epnum == 0 || epnum == 1) { 3787 dwc3_ep0_interrupt(dwc, event); 3788 return; 3789 } 3790 3791 switch (event->endpoint_event) { 3792 case DWC3_DEPEVT_XFERINPROGRESS: 3793 dwc3_gadget_endpoint_transfer_in_progress(dep, event); 3794 break; 3795 case DWC3_DEPEVT_XFERNOTREADY: 3796 dwc3_gadget_endpoint_transfer_not_ready(dep, event); 3797 break; 3798 case DWC3_DEPEVT_EPCMDCMPLT: 3799 dwc3_gadget_endpoint_command_complete(dep, event); 3800 break; 3801 case DWC3_DEPEVT_XFERCOMPLETE: 3802 dwc3_gadget_endpoint_transfer_complete(dep, event); 3803 break; 3804 case DWC3_DEPEVT_STREAMEVT: 3805 dwc3_gadget_endpoint_stream_event(dep, event); 3806 break; 3807 case DWC3_DEPEVT_RXTXFIFOEVT: 3808 break; 3809 default: 3810 dev_err(dwc->dev, "unknown endpoint event %d\n", event->endpoint_event); 3811 break; 3812 } 3813 } 3814 3815 static void dwc3_disconnect_gadget(struct dwc3 *dwc) 3816 { 3817 if (dwc->async_callbacks && dwc->gadget_driver->disconnect) { 3818 spin_unlock(&dwc->lock); 3819 dwc->gadget_driver->disconnect(dwc->gadget); 3820 spin_lock(&dwc->lock); 3821 } 3822 } 3823 3824 static void dwc3_suspend_gadget(struct dwc3 *dwc) 3825 { 3826 if (dwc->async_callbacks && dwc->gadget_driver->suspend) { 3827 spin_unlock(&dwc->lock); 3828 dwc->gadget_driver->suspend(dwc->gadget); 3829 spin_lock(&dwc->lock); 3830 } 3831 } 3832 3833 static void dwc3_resume_gadget(struct dwc3 *dwc) 3834 { 3835 if (dwc->async_callbacks && dwc->gadget_driver->resume) { 3836 spin_unlock(&dwc->lock); 3837 dwc->gadget_driver->resume(dwc->gadget); 3838 spin_lock(&dwc->lock); 3839 } 3840 } 3841 3842 static void dwc3_reset_gadget(struct dwc3 *dwc) 3843 { 3844 if (!dwc->gadget_driver) 3845 return; 3846 3847 if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) { 3848 spin_unlock(&dwc->lock); 3849 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); 3850 spin_lock(&dwc->lock); 3851 } 3852 } 3853 3854 void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, 3855 bool interrupt) 3856 { 3857 struct dwc3 *dwc = dep->dwc; 3858 3859 /* 3860 * Only issue End Transfer command to the control endpoint of a started 3861 * Data Phase. Typically we should only do so in error cases such as 3862 * invalid/unexpected direction as described in the control transfer 3863 * flow of the programming guide. 3864 */ 3865 if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE) 3866 return; 3867 3868 if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP)) 3869 return; 3870 3871 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) || 3872 (dep->flags & DWC3_EP_END_TRANSFER_PENDING)) 3873 return; 3874 3875 /* 3876 * If a Setup packet is received but yet to DMA out, the controller will 3877 * not process the End Transfer command of any endpoint. Polling of its 3878 * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a 3879 * timeout. Delay issuing the End Transfer command until the Setup TRB is 3880 * prepared. 3881 */ 3882 if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) { 3883 dep->flags |= DWC3_EP_DELAY_STOP; 3884 return; 3885 } 3886 3887 /* 3888 * NOTICE: We are violating what the Databook says about the 3889 * EndTransfer command. Ideally we would _always_ wait for the 3890 * EndTransfer Command Completion IRQ, but that's causing too 3891 * much trouble synchronizing between us and gadget driver. 3892 * 3893 * We have discussed this with the IP Provider and it was 3894 * suggested to giveback all requests here. 3895 * 3896 * Note also that a similar handling was tested by Synopsys 3897 * (thanks a lot Paul) and nothing bad has come out of it. 3898 * In short, what we're doing is issuing EndTransfer with 3899 * CMDIOC bit set and delay kicking transfer until the 3900 * EndTransfer command had completed. 3901 * 3902 * As of IP version 3.10a of the DWC_usb3 IP, the controller 3903 * supports a mode to work around the above limitation. The 3904 * software can poll the CMDACT bit in the DEPCMD register 3905 * after issuing a EndTransfer command. This mode is enabled 3906 * by writing GUCTL2[14]. This polling is already done in the 3907 * dwc3_send_gadget_ep_cmd() function so if the mode is 3908 * enabled, the EndTransfer command will have completed upon 3909 * returning from this function. 3910 * 3911 * This mode is NOT available on the DWC_usb31 IP. In this 3912 * case, if the IOC bit is not set, then delay by 1ms 3913 * after issuing the EndTransfer command. This allows for the 3914 * controller to handle the command completely before DWC3 3915 * remove requests attempts to unmap USB request buffers. 3916 */ 3917 3918 __dwc3_stop_active_transfer(dep, force, interrupt); 3919 } 3920 3921 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) 3922 { 3923 u32 epnum; 3924 3925 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 3926 struct dwc3_ep *dep; 3927 int ret; 3928 3929 dep = dwc->eps[epnum]; 3930 if (!dep) 3931 continue; 3932 3933 if (!(dep->flags & DWC3_EP_STALL)) 3934 continue; 3935 3936 dep->flags &= ~DWC3_EP_STALL; 3937 3938 ret = dwc3_send_clear_stall_ep_cmd(dep); 3939 WARN_ON_ONCE(ret); 3940 } 3941 } 3942 3943 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) 3944 { 3945 int reg; 3946 3947 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET); 3948 3949 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 3950 reg &= ~DWC3_DCTL_INITU1ENA; 3951 reg &= ~DWC3_DCTL_INITU2ENA; 3952 dwc3_gadget_dctl_write_safe(dwc, reg); 3953 3954 dwc->connected = false; 3955 3956 dwc3_disconnect_gadget(dwc); 3957 3958 dwc->gadget->speed = USB_SPEED_UNKNOWN; 3959 dwc->setup_packet_pending = false; 3960 dwc->gadget->wakeup_armed = false; 3961 dwc3_gadget_enable_linksts_evts(dwc, false); 3962 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); 3963 3964 dwc3_ep0_reset_state(dwc); 3965 } 3966 3967 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) 3968 { 3969 u32 reg; 3970 3971 /* 3972 * Ideally, dwc3_reset_gadget() would trigger the function 3973 * drivers to stop any active transfers through ep disable. 3974 * However, for functions which defer ep disable, such as mass 3975 * storage, we will need to rely on the call to stop active 3976 * transfers here, and avoid allowing of request queuing. 3977 */ 3978 dwc->connected = false; 3979 3980 /* 3981 * WORKAROUND: DWC3 revisions <1.88a have an issue which 3982 * would cause a missing Disconnect Event if there's a 3983 * pending Setup Packet in the FIFO. 3984 * 3985 * There's no suggested workaround on the official Bug 3986 * report, which states that "unless the driver/application 3987 * is doing any special handling of a disconnect event, 3988 * there is no functional issue". 3989 * 3990 * Unfortunately, it turns out that we _do_ some special 3991 * handling of a disconnect event, namely complete all 3992 * pending transfers, notify gadget driver of the 3993 * disconnection, and so on. 3994 * 3995 * Our suggested workaround is to follow the Disconnect 3996 * Event steps here, instead, based on a setup_packet_pending 3997 * flag. Such flag gets set whenever we have a SETUP_PENDING 3998 * status for EP0 TRBs and gets cleared on XferComplete for the 3999 * same endpoint. 4000 * 4001 * Refers to: 4002 * 4003 * STAR#9000466709: RTL: Device : Disconnect event not 4004 * generated if setup packet pending in FIFO 4005 */ 4006 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) { 4007 if (dwc->setup_packet_pending) 4008 dwc3_gadget_disconnect_interrupt(dwc); 4009 } 4010 4011 dwc3_reset_gadget(dwc); 4012 4013 /* 4014 * From SNPS databook section 8.1.2, the EP0 should be in setup 4015 * phase. So ensure that EP0 is in setup phase by issuing a stall 4016 * and restart if EP0 is not in setup phase. 4017 */ 4018 dwc3_ep0_reset_state(dwc); 4019 4020 /* 4021 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a 4022 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW 4023 * needs to ensure that it sends "a DEPENDXFER command for any active 4024 * transfers." 4025 */ 4026 dwc3_stop_active_transfers(dwc); 4027 dwc->connected = true; 4028 4029 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4030 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 4031 dwc3_gadget_dctl_write_safe(dwc, reg); 4032 dwc->test_mode = false; 4033 dwc->gadget->wakeup_armed = false; 4034 dwc3_gadget_enable_linksts_evts(dwc, false); 4035 dwc3_clear_stall_all_ep(dwc); 4036 4037 /* Reset device address to zero */ 4038 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 4039 reg &= ~(DWC3_DCFG_DEVADDR_MASK); 4040 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 4041 } 4042 4043 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) 4044 { 4045 struct dwc3_ep *dep; 4046 int ret; 4047 u32 reg; 4048 u8 lanes = 1; 4049 u8 speed; 4050 4051 if (!dwc->softconnect) 4052 return; 4053 4054 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 4055 speed = reg & DWC3_DSTS_CONNECTSPD; 4056 dwc->speed = speed; 4057 4058 if (DWC3_IP_IS(DWC32)) 4059 lanes = DWC3_DSTS_CONNLANES(reg) + 1; 4060 4061 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; 4062 4063 /* 4064 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed 4065 * each time on Connect Done. 4066 * 4067 * Currently we always use the reset value. If any platform 4068 * wants to set this to a different value, we need to add a 4069 * setting and update GCTL.RAMCLKSEL here. 4070 */ 4071 4072 switch (speed) { 4073 case DWC3_DSTS_SUPERSPEED_PLUS: 4074 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 4075 dwc->gadget->ep0->maxpacket = 512; 4076 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; 4077 4078 if (lanes > 1) 4079 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2; 4080 else 4081 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1; 4082 break; 4083 case DWC3_DSTS_SUPERSPEED: 4084 /* 4085 * WORKAROUND: DWC3 revisions <1.90a have an issue which 4086 * would cause a missing USB3 Reset event. 4087 * 4088 * In such situations, we should force a USB3 Reset 4089 * event by calling our dwc3_gadget_reset_interrupt() 4090 * routine. 4091 * 4092 * Refers to: 4093 * 4094 * STAR#9000483510: RTL: SS : USB3 reset event may 4095 * not be generated always when the link enters poll 4096 */ 4097 if (DWC3_VER_IS_PRIOR(DWC3, 190A)) 4098 dwc3_gadget_reset_interrupt(dwc); 4099 4100 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 4101 dwc->gadget->ep0->maxpacket = 512; 4102 dwc->gadget->speed = USB_SPEED_SUPER; 4103 4104 if (lanes > 1) { 4105 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; 4106 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2; 4107 } 4108 break; 4109 case DWC3_DSTS_HIGHSPEED: 4110 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 4111 dwc->gadget->ep0->maxpacket = 64; 4112 dwc->gadget->speed = USB_SPEED_HIGH; 4113 break; 4114 case DWC3_DSTS_FULLSPEED: 4115 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 4116 dwc->gadget->ep0->maxpacket = 64; 4117 dwc->gadget->speed = USB_SPEED_FULL; 4118 break; 4119 } 4120 4121 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; 4122 4123 /* Enable USB2 LPM Capability */ 4124 4125 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) && 4126 !dwc->usb2_gadget_lpm_disable && 4127 (speed != DWC3_DSTS_SUPERSPEED) && 4128 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { 4129 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 4130 reg |= DWC3_DCFG_LPM_CAP; 4131 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 4132 4133 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4134 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); 4135 4136 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold | 4137 (dwc->is_utmi_l1_suspend << 4)); 4138 4139 /* 4140 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and 4141 * DCFG.LPMCap is set, core responses with an ACK and the 4142 * BESL value in the LPM token is less than or equal to LPM 4143 * NYET threshold. 4144 */ 4145 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum, 4146 "LPM Erratum not available on dwc3 revisions < 2.40a\n"); 4147 4148 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) 4149 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold); 4150 4151 dwc3_gadget_dctl_write_safe(dwc, reg); 4152 } else { 4153 if (dwc->usb2_gadget_lpm_disable) { 4154 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 4155 reg &= ~DWC3_DCFG_LPM_CAP; 4156 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 4157 } 4158 4159 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4160 reg &= ~DWC3_DCTL_HIRD_THRES_MASK; 4161 dwc3_gadget_dctl_write_safe(dwc, reg); 4162 } 4163 4164 dep = dwc->eps[0]; 4165 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 4166 if (ret) { 4167 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 4168 return; 4169 } 4170 4171 dep = dwc->eps[1]; 4172 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 4173 if (ret) { 4174 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 4175 return; 4176 } 4177 4178 /* 4179 * Configure PHY via GUSB3PIPECTLn if required. 4180 * 4181 * Update GTXFIFOSIZn 4182 * 4183 * In both cases reset values should be sufficient. 4184 */ 4185 } 4186 4187 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, unsigned int evtinfo) 4188 { 4189 /* 4190 * TODO take core out of low power mode when that's 4191 * implemented. 4192 */ 4193 4194 if (dwc->async_callbacks && dwc->gadget_driver->resume) { 4195 spin_unlock(&dwc->lock); 4196 dwc->gadget_driver->resume(dwc->gadget); 4197 spin_lock(&dwc->lock); 4198 } 4199 4200 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK; 4201 } 4202 4203 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, 4204 unsigned int evtinfo) 4205 { 4206 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 4207 unsigned int pwropt; 4208 4209 /* 4210 * WORKAROUND: DWC3 < 2.50a have an issue when configured without 4211 * Hibernation mode enabled which would show up when device detects 4212 * host-initiated U3 exit. 4213 * 4214 * In that case, device will generate a Link State Change Interrupt 4215 * from U3 to RESUME which is only necessary if Hibernation is 4216 * configured in. 4217 * 4218 * There are no functional changes due to such spurious event and we 4219 * just need to ignore it. 4220 * 4221 * Refers to: 4222 * 4223 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation 4224 * operational mode 4225 */ 4226 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); 4227 if (DWC3_VER_IS_PRIOR(DWC3, 250A) && 4228 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { 4229 if ((dwc->link_state == DWC3_LINK_STATE_U3) && 4230 (next == DWC3_LINK_STATE_RESUME)) { 4231 return; 4232 } 4233 } 4234 4235 /* 4236 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending 4237 * on the link partner, the USB session might do multiple entry/exit 4238 * of low power states before a transfer takes place. 4239 * 4240 * Due to this problem, we might experience lower throughput. The 4241 * suggested workaround is to disable DCTL[12:9] bits if we're 4242 * transitioning from U1/U2 to U0 and enable those bits again 4243 * after a transfer completes and there are no pending transfers 4244 * on any of the enabled endpoints. 4245 * 4246 * This is the first half of that workaround. 4247 * 4248 * Refers to: 4249 * 4250 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us 4251 * core send LGO_Ux entering U0 4252 */ 4253 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { 4254 if (next == DWC3_LINK_STATE_U0) { 4255 u32 u1u2; 4256 u32 reg; 4257 4258 switch (dwc->link_state) { 4259 case DWC3_LINK_STATE_U1: 4260 case DWC3_LINK_STATE_U2: 4261 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 4262 u1u2 = reg & (DWC3_DCTL_INITU2ENA 4263 | DWC3_DCTL_ACCEPTU2ENA 4264 | DWC3_DCTL_INITU1ENA 4265 | DWC3_DCTL_ACCEPTU1ENA); 4266 4267 if (!dwc->u1u2) 4268 dwc->u1u2 = reg & u1u2; 4269 4270 reg &= ~u1u2; 4271 4272 dwc3_gadget_dctl_write_safe(dwc, reg); 4273 break; 4274 default: 4275 /* do nothing */ 4276 break; 4277 } 4278 } 4279 } 4280 4281 switch (next) { 4282 case DWC3_LINK_STATE_U0: 4283 if (dwc->gadget->wakeup_armed) { 4284 dwc3_gadget_enable_linksts_evts(dwc, false); 4285 dwc3_resume_gadget(dwc); 4286 } 4287 break; 4288 case DWC3_LINK_STATE_U1: 4289 if (dwc->speed == USB_SPEED_SUPER) 4290 dwc3_suspend_gadget(dwc); 4291 break; 4292 case DWC3_LINK_STATE_U2: 4293 case DWC3_LINK_STATE_U3: 4294 dwc3_suspend_gadget(dwc); 4295 break; 4296 case DWC3_LINK_STATE_RESUME: 4297 dwc3_resume_gadget(dwc); 4298 break; 4299 default: 4300 /* do nothing */ 4301 break; 4302 } 4303 4304 dwc->link_state = next; 4305 } 4306 4307 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, 4308 unsigned int evtinfo) 4309 { 4310 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 4311 4312 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) 4313 dwc3_suspend_gadget(dwc); 4314 4315 dwc->link_state = next; 4316 } 4317 4318 static void dwc3_gadget_interrupt(struct dwc3 *dwc, 4319 const struct dwc3_event_devt *event) 4320 { 4321 switch (event->type) { 4322 case DWC3_DEVICE_EVENT_DISCONNECT: 4323 dwc3_gadget_disconnect_interrupt(dwc); 4324 break; 4325 case DWC3_DEVICE_EVENT_RESET: 4326 dwc3_gadget_reset_interrupt(dwc); 4327 break; 4328 case DWC3_DEVICE_EVENT_CONNECT_DONE: 4329 dwc3_gadget_conndone_interrupt(dwc); 4330 break; 4331 case DWC3_DEVICE_EVENT_WAKEUP: 4332 dwc3_gadget_wakeup_interrupt(dwc, event->event_info); 4333 break; 4334 case DWC3_DEVICE_EVENT_HIBER_REQ: 4335 dev_WARN_ONCE(dwc->dev, true, "unexpected hibernation event\n"); 4336 break; 4337 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 4338 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); 4339 break; 4340 case DWC3_DEVICE_EVENT_SUSPEND: 4341 /* It changed to be suspend event for version 2.30a and above */ 4342 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) 4343 dwc3_gadget_suspend_interrupt(dwc, event->event_info); 4344 break; 4345 case DWC3_DEVICE_EVENT_SOF: 4346 case DWC3_DEVICE_EVENT_ERRATIC_ERROR: 4347 case DWC3_DEVICE_EVENT_CMD_CMPL: 4348 case DWC3_DEVICE_EVENT_OVERFLOW: 4349 break; 4350 default: 4351 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); 4352 } 4353 } 4354 4355 static void dwc3_process_event_entry(struct dwc3 *dwc, 4356 const union dwc3_event *event) 4357 { 4358 trace_dwc3_event(event->raw, dwc); 4359 4360 if (!event->type.is_devspec) 4361 dwc3_endpoint_interrupt(dwc, &event->depevt); 4362 else if (event->type.type == DWC3_EVENT_TYPE_DEV) 4363 dwc3_gadget_interrupt(dwc, &event->devt); 4364 else 4365 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); 4366 } 4367 4368 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) 4369 { 4370 struct dwc3 *dwc = evt->dwc; 4371 irqreturn_t ret = IRQ_NONE; 4372 int left; 4373 4374 left = evt->count; 4375 4376 if (!(evt->flags & DWC3_EVENT_PENDING)) 4377 return IRQ_NONE; 4378 4379 while (left > 0) { 4380 union dwc3_event event; 4381 4382 event.raw = *(u32 *) (evt->cache + evt->lpos); 4383 4384 dwc3_process_event_entry(dwc, &event); 4385 4386 /* 4387 * FIXME we wrap around correctly to the next entry as 4388 * almost all entries are 4 bytes in size. There is one 4389 * entry which has 12 bytes which is a regular entry 4390 * followed by 8 bytes data. ATM I don't know how 4391 * things are organized if we get next to the a 4392 * boundary so I worry about that once we try to handle 4393 * that. 4394 */ 4395 evt->lpos = (evt->lpos + 4) % evt->length; 4396 left -= 4; 4397 } 4398 4399 evt->count = 0; 4400 ret = IRQ_HANDLED; 4401 4402 /* Unmask interrupt */ 4403 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), 4404 DWC3_GEVNTSIZ_SIZE(evt->length)); 4405 4406 if (dwc->imod_interval) { 4407 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 4408 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); 4409 } 4410 4411 /* Keep the clearing of DWC3_EVENT_PENDING at the end */ 4412 evt->flags &= ~DWC3_EVENT_PENDING; 4413 4414 return ret; 4415 } 4416 4417 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) 4418 { 4419 struct dwc3_event_buffer *evt = _evt; 4420 struct dwc3 *dwc = evt->dwc; 4421 unsigned long flags; 4422 irqreturn_t ret = IRQ_NONE; 4423 4424 local_bh_disable(); 4425 spin_lock_irqsave(&dwc->lock, flags); 4426 ret = dwc3_process_event_buf(evt); 4427 spin_unlock_irqrestore(&dwc->lock, flags); 4428 local_bh_enable(); 4429 4430 return ret; 4431 } 4432 4433 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) 4434 { 4435 struct dwc3 *dwc = evt->dwc; 4436 u32 amount; 4437 u32 count; 4438 4439 if (pm_runtime_suspended(dwc->dev)) { 4440 pm_runtime_get(dwc->dev); 4441 disable_irq_nosync(dwc->irq_gadget); 4442 dwc->pending_events = true; 4443 return IRQ_HANDLED; 4444 } 4445 4446 /* 4447 * With PCIe legacy interrupt, test shows that top-half irq handler can 4448 * be called again after HW interrupt deassertion. Check if bottom-half 4449 * irq event handler completes before caching new event to prevent 4450 * losing events. 4451 */ 4452 if (evt->flags & DWC3_EVENT_PENDING) 4453 return IRQ_HANDLED; 4454 4455 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); 4456 count &= DWC3_GEVNTCOUNT_MASK; 4457 if (!count) 4458 return IRQ_NONE; 4459 4460 evt->count = count; 4461 evt->flags |= DWC3_EVENT_PENDING; 4462 4463 /* Mask interrupt */ 4464 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), 4465 DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length)); 4466 4467 amount = min(count, evt->length - evt->lpos); 4468 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); 4469 4470 if (amount < count) 4471 memcpy(evt->cache, evt->buf, count - amount); 4472 4473 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); 4474 4475 return IRQ_WAKE_THREAD; 4476 } 4477 4478 static irqreturn_t dwc3_interrupt(int irq, void *_evt) 4479 { 4480 struct dwc3_event_buffer *evt = _evt; 4481 4482 return dwc3_check_event_buf(evt); 4483 } 4484 4485 static int dwc3_gadget_get_irq(struct dwc3 *dwc) 4486 { 4487 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); 4488 int irq; 4489 4490 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral"); 4491 if (irq > 0) 4492 goto out; 4493 4494 if (irq == -EPROBE_DEFER) 4495 goto out; 4496 4497 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3"); 4498 if (irq > 0) 4499 goto out; 4500 4501 if (irq == -EPROBE_DEFER) 4502 goto out; 4503 4504 irq = platform_get_irq(dwc3_pdev, 0); 4505 4506 out: 4507 return irq; 4508 } 4509 4510 static void dwc_gadget_release(struct device *dev) 4511 { 4512 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); 4513 4514 kfree(gadget); 4515 } 4516 4517 /** 4518 * dwc3_gadget_init - initializes gadget related registers 4519 * @dwc: pointer to our controller context structure 4520 * 4521 * Returns 0 on success otherwise negative errno. 4522 */ 4523 int dwc3_gadget_init(struct dwc3 *dwc) 4524 { 4525 int ret; 4526 int irq; 4527 struct device *dev; 4528 4529 irq = dwc3_gadget_get_irq(dwc); 4530 if (irq < 0) { 4531 ret = irq; 4532 goto err0; 4533 } 4534 4535 dwc->irq_gadget = irq; 4536 4537 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, 4538 sizeof(*dwc->ep0_trb) * 2, 4539 &dwc->ep0_trb_addr, GFP_KERNEL); 4540 if (!dwc->ep0_trb) { 4541 dev_err(dwc->dev, "failed to allocate ep0 trb\n"); 4542 ret = -ENOMEM; 4543 goto err0; 4544 } 4545 4546 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); 4547 if (!dwc->setup_buf) { 4548 ret = -ENOMEM; 4549 goto err1; 4550 } 4551 4552 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, 4553 &dwc->bounce_addr, GFP_KERNEL); 4554 if (!dwc->bounce) { 4555 ret = -ENOMEM; 4556 goto err2; 4557 } 4558 4559 init_completion(&dwc->ep0_in_setup); 4560 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL); 4561 if (!dwc->gadget) { 4562 ret = -ENOMEM; 4563 goto err3; 4564 } 4565 4566 4567 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); 4568 dev = &dwc->gadget->dev; 4569 dev->platform_data = dwc; 4570 dwc->gadget->ops = &dwc3_gadget_ops; 4571 dwc->gadget->speed = USB_SPEED_UNKNOWN; 4572 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; 4573 dwc->gadget->sg_supported = true; 4574 dwc->gadget->name = "dwc3-gadget"; 4575 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable; 4576 dwc->gadget->wakeup_capable = true; 4577 4578 /* 4579 * FIXME We might be setting max_speed to <SUPER, however versions 4580 * <2.20a of dwc3 have an issue with metastability (documented 4581 * elsewhere in this driver) which tells us we can't set max speed to 4582 * anything lower than SUPER. 4583 * 4584 * Because gadget.max_speed is only used by composite.c and function 4585 * drivers (i.e. it won't go into dwc3's registers) we are allowing this 4586 * to happen so we avoid sending SuperSpeed Capability descriptor 4587 * together with our BOS descriptor as that could confuse host into 4588 * thinking we can handle super speed. 4589 * 4590 * Note that, in fact, we won't even support GetBOS requests when speed 4591 * is less than super speed because we don't have means, yet, to tell 4592 * composite.c that we are USB 2.0 + LPM ECN. 4593 */ 4594 if (DWC3_VER_IS_PRIOR(DWC3, 220A) && 4595 !dwc->dis_metastability_quirk) 4596 dev_info(dwc->dev, "changing max_speed on rev %08x\n", 4597 dwc->revision); 4598 4599 dwc->gadget->max_speed = dwc->maximum_speed; 4600 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate; 4601 4602 /* 4603 * REVISIT: Here we should clear all pending IRQs to be 4604 * sure we're starting from a well known location. 4605 */ 4606 4607 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); 4608 if (ret) 4609 goto err4; 4610 4611 ret = usb_add_gadget(dwc->gadget); 4612 if (ret) { 4613 dev_err(dwc->dev, "failed to add gadget\n"); 4614 goto err5; 4615 } 4616 4617 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS) 4618 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate); 4619 else 4620 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); 4621 4622 return 0; 4623 4624 err5: 4625 dwc3_gadget_free_endpoints(dwc); 4626 err4: 4627 usb_put_gadget(dwc->gadget); 4628 dwc->gadget = NULL; 4629 err3: 4630 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 4631 dwc->bounce_addr); 4632 4633 err2: 4634 kfree(dwc->setup_buf); 4635 4636 err1: 4637 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 4638 dwc->ep0_trb, dwc->ep0_trb_addr); 4639 4640 err0: 4641 return ret; 4642 } 4643 4644 /* -------------------------------------------------------------------------- */ 4645 4646 void dwc3_gadget_exit(struct dwc3 *dwc) 4647 { 4648 if (!dwc->gadget) 4649 return; 4650 4651 usb_del_gadget(dwc->gadget); 4652 dwc3_gadget_free_endpoints(dwc); 4653 usb_put_gadget(dwc->gadget); 4654 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 4655 dwc->bounce_addr); 4656 kfree(dwc->setup_buf); 4657 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 4658 dwc->ep0_trb, dwc->ep0_trb_addr); 4659 } 4660 4661 int dwc3_gadget_suspend(struct dwc3 *dwc) 4662 { 4663 unsigned long flags; 4664 4665 if (!dwc->gadget_driver) 4666 return 0; 4667 4668 dwc3_gadget_run_stop(dwc, false); 4669 4670 spin_lock_irqsave(&dwc->lock, flags); 4671 dwc3_disconnect_gadget(dwc); 4672 __dwc3_gadget_stop(dwc); 4673 spin_unlock_irqrestore(&dwc->lock, flags); 4674 4675 return 0; 4676 } 4677 4678 int dwc3_gadget_resume(struct dwc3 *dwc) 4679 { 4680 int ret; 4681 4682 if (!dwc->gadget_driver || !dwc->softconnect) 4683 return 0; 4684 4685 ret = __dwc3_gadget_start(dwc); 4686 if (ret < 0) 4687 goto err0; 4688 4689 ret = dwc3_gadget_run_stop(dwc, true); 4690 if (ret < 0) 4691 goto err1; 4692 4693 return 0; 4694 4695 err1: 4696 __dwc3_gadget_stop(dwc); 4697 4698 err0: 4699 return ret; 4700 } 4701 4702 void dwc3_gadget_process_pending_events(struct dwc3 *dwc) 4703 { 4704 if (dwc->pending_events) { 4705 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); 4706 dwc->pending_events = false; 4707 enable_irq(dwc->irq_gadget); 4708 } 4709 } 4710