1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Cadence USBSS DRD Driver - gadget side. 4 * 5 * Copyright (C) 2018-2019 Cadence Design Systems. 6 * Copyright (C) 2017-2018 NXP 7 * 8 * Authors: Pawel Jez <pjez@cadence.com>, 9 * Pawel Laszczak <pawell@cadence.com> 10 * Peter Chen <peter.chen@nxp.com> 11 */ 12 13 /* 14 * Work around 1: 15 * At some situations, the controller may get stale data address in TRB 16 * at below sequences: 17 * 1. Controller read TRB includes data address 18 * 2. Software updates TRBs includes data address and Cycle bit 19 * 3. Controller read TRB which includes Cycle bit 20 * 4. DMA run with stale data address 21 * 22 * To fix this problem, driver needs to make the first TRB in TD as invalid. 23 * After preparing all TRBs driver needs to check the position of DMA and 24 * if the DMA point to the first just added TRB and doorbell is 1, 25 * then driver must defer making this TRB as valid. This TRB will be make 26 * as valid during adding next TRB only if DMA is stopped or at TRBERR 27 * interrupt. 28 * 29 * Issue has been fixed in DEV_VER_V3 version of controller. 30 * 31 * Work around 2: 32 * Controller for OUT endpoints has shared on-chip buffers for all incoming 33 * packets, including ep0out. It's FIFO buffer, so packets must be handle by DMA 34 * in correct order. If the first packet in the buffer will not be handled, 35 * then the following packets directed for other endpoints and functions 36 * will be blocked. 37 * Additionally the packets directed to one endpoint can block entire on-chip 38 * buffers. In this case transfer to other endpoints also will blocked. 39 * 40 * To resolve this issue after raising the descriptor missing interrupt 41 * driver prepares internal usb_request object and use it to arm DMA transfer. 42 * 43 * The problematic situation was observed in case when endpoint has been enabled 44 * but no usb_request were queued. Driver try detects such endpoints and will 45 * use this workaround only for these endpoint. 46 * 47 * Driver use limited number of buffer. This number can be set by macro 48 * CDNS3_WA2_NUM_BUFFERS. 49 * 50 * Such blocking situation was observed on ACM gadget. For this function 51 * host send OUT data packet but ACM function is not prepared for this packet. 52 * It's cause that buffer placed in on chip memory block transfer to other 53 * endpoints. 54 * 55 * Issue has been fixed in DEV_VER_V2 version of controller. 56 * 57 */ 58 59 #include <linux/dma-mapping.h> 60 #include <linux/usb/gadget.h> 61 #include <linux/module.h> 62 #include <linux/dmapool.h> 63 #include <linux/iopoll.h> 64 #include <linux/property.h> 65 66 #include "core.h" 67 #include "gadget-export.h" 68 #include "cdns3-gadget.h" 69 #include "cdns3-trace.h" 70 #include "drd.h" 71 72 static int __cdns3_gadget_ep_queue(struct usb_ep *ep, 73 struct usb_request *request, 74 gfp_t gfp_flags); 75 76 static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep, 77 struct usb_request *request); 78 79 static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep, 80 struct usb_request *request); 81 82 /** 83 * cdns3_clear_register_bit - clear bit in given register. 84 * @ptr: address of device controller register to be read and changed 85 * @mask: bits requested to clar 86 */ 87 static void cdns3_clear_register_bit(void __iomem *ptr, u32 mask) 88 { 89 mask = readl(ptr) & ~mask; 90 writel(mask, ptr); 91 } 92 93 /** 94 * cdns3_set_register_bit - set bit in given register. 95 * @ptr: address of device controller register to be read and changed 96 * @mask: bits requested to set 97 */ 98 void cdns3_set_register_bit(void __iomem *ptr, u32 mask) 99 { 100 mask = readl(ptr) | mask; 101 writel(mask, ptr); 102 } 103 104 /** 105 * cdns3_ep_addr_to_index - Macro converts endpoint address to 106 * index of endpoint object in cdns3_device.eps[] container 107 * @ep_addr: endpoint address for which endpoint object is required 108 * 109 */ 110 u8 cdns3_ep_addr_to_index(u8 ep_addr) 111 { 112 return (((ep_addr & 0x7F)) + ((ep_addr & USB_DIR_IN) ? 16 : 0)); 113 } 114 115 static int cdns3_get_dma_pos(struct cdns3_device *priv_dev, 116 struct cdns3_endpoint *priv_ep) 117 { 118 int dma_index; 119 120 dma_index = readl(&priv_dev->regs->ep_traddr) - priv_ep->trb_pool_dma; 121 122 return dma_index / TRB_SIZE; 123 } 124 125 /** 126 * cdns3_next_request - returns next request from list 127 * @list: list containing requests 128 * 129 * Returns request or NULL if no requests in list 130 */ 131 struct usb_request *cdns3_next_request(struct list_head *list) 132 { 133 return list_first_entry_or_null(list, struct usb_request, list); 134 } 135 136 /** 137 * cdns3_next_align_buf - returns next buffer from list 138 * @list: list containing buffers 139 * 140 * Returns buffer or NULL if no buffers in list 141 */ 142 static struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list) 143 { 144 return list_first_entry_or_null(list, struct cdns3_aligned_buf, list); 145 } 146 147 /** 148 * cdns3_next_priv_request - returns next request from list 149 * @list: list containing requests 150 * 151 * Returns request or NULL if no requests in list 152 */ 153 static struct cdns3_request *cdns3_next_priv_request(struct list_head *list) 154 { 155 return list_first_entry_or_null(list, struct cdns3_request, list); 156 } 157 158 /** 159 * cdns3_select_ep - selects endpoint 160 * @priv_dev: extended gadget object 161 * @ep: endpoint address 162 */ 163 void cdns3_select_ep(struct cdns3_device *priv_dev, u32 ep) 164 { 165 if (priv_dev->selected_ep == ep) 166 return; 167 168 priv_dev->selected_ep = ep; 169 writel(ep, &priv_dev->regs->ep_sel); 170 } 171 172 /** 173 * cdns3_get_tdl - gets current tdl for selected endpoint. 174 * @priv_dev: extended gadget object 175 * 176 * Before calling this function the appropriate endpoint must 177 * be selected by means of cdns3_select_ep function. 178 */ 179 static int cdns3_get_tdl(struct cdns3_device *priv_dev) 180 { 181 if (priv_dev->dev_ver < DEV_VER_V3) 182 return EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd)); 183 else 184 return readl(&priv_dev->regs->ep_tdl); 185 } 186 187 dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep, 188 struct cdns3_trb *trb) 189 { 190 u32 offset = (char *)trb - (char *)priv_ep->trb_pool; 191 192 return priv_ep->trb_pool_dma + offset; 193 } 194 195 static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep) 196 { 197 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 198 199 if (priv_ep->trb_pool) { 200 dma_pool_free(priv_dev->eps_dma_pool, 201 priv_ep->trb_pool, priv_ep->trb_pool_dma); 202 priv_ep->trb_pool = NULL; 203 } 204 } 205 206 /** 207 * cdns3_allocate_trb_pool - Allocates TRB's pool for selected endpoint 208 * @priv_ep: endpoint object 209 * 210 * Function will return 0 on success or -ENOMEM on allocation error 211 */ 212 int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep) 213 { 214 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 215 int ring_size = TRB_RING_SIZE; 216 int num_trbs = ring_size / TRB_SIZE; 217 struct cdns3_trb *link_trb; 218 219 if (priv_ep->trb_pool && priv_ep->alloc_ring_size < ring_size) 220 cdns3_free_trb_pool(priv_ep); 221 222 if (!priv_ep->trb_pool) { 223 priv_ep->trb_pool = dma_pool_alloc(priv_dev->eps_dma_pool, 224 GFP_ATOMIC, 225 &priv_ep->trb_pool_dma); 226 227 if (!priv_ep->trb_pool) 228 return -ENOMEM; 229 230 priv_ep->alloc_ring_size = ring_size; 231 } 232 233 memset(priv_ep->trb_pool, 0, ring_size); 234 235 priv_ep->num_trbs = num_trbs; 236 237 if (!priv_ep->num) 238 return 0; 239 240 /* Initialize the last TRB as Link TRB */ 241 link_trb = (priv_ep->trb_pool + (priv_ep->num_trbs - 1)); 242 243 if (priv_ep->use_streams) { 244 /* 245 * For stream capable endpoints driver use single correct TRB. 246 * The last trb has zeroed cycle bit 247 */ 248 link_trb->control = 0; 249 } else { 250 link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma)); 251 link_trb->control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_LINK) | TRB_TOGGLE); 252 } 253 return 0; 254 } 255 256 /** 257 * cdns3_ep_stall_flush - Stalls and flushes selected endpoint 258 * @priv_ep: endpoint object 259 * 260 * Endpoint must be selected before call to this function 261 */ 262 static void cdns3_ep_stall_flush(struct cdns3_endpoint *priv_ep) 263 { 264 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 265 int val; 266 267 trace_cdns3_halt(priv_ep, 1, 1); 268 269 writel(EP_CMD_DFLUSH | EP_CMD_ERDY | EP_CMD_SSTALL, 270 &priv_dev->regs->ep_cmd); 271 272 /* wait for DFLUSH cleared */ 273 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val, 274 !(val & EP_CMD_DFLUSH), 1, 1000); 275 priv_ep->flags |= EP_STALLED; 276 priv_ep->flags &= ~EP_STALL_PENDING; 277 } 278 279 /** 280 * cdns3_hw_reset_eps_config - reset endpoints configuration kept by controller. 281 * @priv_dev: extended gadget object 282 */ 283 void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev) 284 { 285 int i; 286 287 writel(USB_CONF_CFGRST, &priv_dev->regs->usb_conf); 288 289 cdns3_allow_enable_l1(priv_dev, 0); 290 priv_dev->hw_configured_flag = 0; 291 priv_dev->onchip_used_size = 0; 292 priv_dev->out_mem_is_allocated = 0; 293 priv_dev->wait_for_setup = 0; 294 priv_dev->using_streams = 0; 295 296 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) 297 if (priv_dev->eps[i]) 298 priv_dev->eps[i]->flags &= ~EP_CONFIGURED; 299 } 300 301 /** 302 * cdns3_ep_inc_trb - increment a trb index. 303 * @index: Pointer to the TRB index to increment. 304 * @cs: Cycle state 305 * @trb_in_seg: number of TRBs in segment 306 * 307 * The index should never point to the link TRB. After incrementing, 308 * if it is point to the link TRB, wrap around to the beginning and revert 309 * cycle state bit The 310 * link TRB is always at the last TRB entry. 311 */ 312 static void cdns3_ep_inc_trb(int *index, u8 *cs, int trb_in_seg) 313 { 314 (*index)++; 315 if (*index == (trb_in_seg - 1)) { 316 *index = 0; 317 *cs ^= 1; 318 } 319 } 320 321 /** 322 * cdns3_ep_inc_enq - increment endpoint's enqueue pointer 323 * @priv_ep: The endpoint whose enqueue pointer we're incrementing 324 */ 325 static void cdns3_ep_inc_enq(struct cdns3_endpoint *priv_ep) 326 { 327 priv_ep->free_trbs--; 328 cdns3_ep_inc_trb(&priv_ep->enqueue, &priv_ep->pcs, priv_ep->num_trbs); 329 } 330 331 /** 332 * cdns3_ep_inc_deq - increment endpoint's dequeue pointer 333 * @priv_ep: The endpoint whose dequeue pointer we're incrementing 334 */ 335 static void cdns3_ep_inc_deq(struct cdns3_endpoint *priv_ep) 336 { 337 priv_ep->free_trbs++; 338 cdns3_ep_inc_trb(&priv_ep->dequeue, &priv_ep->ccs, priv_ep->num_trbs); 339 } 340 341 /** 342 * cdns3_allow_enable_l1 - enable/disable permits to transition to L1. 343 * @priv_dev: Extended gadget object 344 * @enable: Enable/disable permit to transition to L1. 345 * 346 * If bit USB_CONF_L1EN is set and device receive Extended Token packet, 347 * then controller answer with ACK handshake. 348 * If bit USB_CONF_L1DS is set and device receive Extended Token packet, 349 * then controller answer with NYET handshake. 350 */ 351 void cdns3_allow_enable_l1(struct cdns3_device *priv_dev, int enable) 352 { 353 if (enable) 354 writel(USB_CONF_L1EN, &priv_dev->regs->usb_conf); 355 else 356 writel(USB_CONF_L1DS, &priv_dev->regs->usb_conf); 357 } 358 359 enum usb_device_speed cdns3_get_speed(struct cdns3_device *priv_dev) 360 { 361 u32 reg; 362 363 reg = readl(&priv_dev->regs->usb_sts); 364 365 if (DEV_SUPERSPEED(reg)) 366 return USB_SPEED_SUPER; 367 else if (DEV_HIGHSPEED(reg)) 368 return USB_SPEED_HIGH; 369 else if (DEV_FULLSPEED(reg)) 370 return USB_SPEED_FULL; 371 else if (DEV_LOWSPEED(reg)) 372 return USB_SPEED_LOW; 373 return USB_SPEED_UNKNOWN; 374 } 375 376 /** 377 * cdns3_start_all_request - add to ring all request not started 378 * @priv_dev: Extended gadget object 379 * @priv_ep: The endpoint for whom request will be started. 380 * 381 * Returns return ENOMEM if transfer ring i not enough TRBs to start 382 * all requests. 383 */ 384 static int cdns3_start_all_request(struct cdns3_device *priv_dev, 385 struct cdns3_endpoint *priv_ep) 386 { 387 struct usb_request *request; 388 int ret = 0; 389 u8 pending_empty = list_empty(&priv_ep->pending_req_list); 390 391 /* 392 * If the last pending transfer is INTERNAL 393 * OR streams are enabled for this endpoint 394 * do NOT start new transfer till the last one is pending 395 */ 396 if (!pending_empty) { 397 struct cdns3_request *priv_req; 398 399 request = cdns3_next_request(&priv_ep->pending_req_list); 400 priv_req = to_cdns3_request(request); 401 if ((priv_req->flags & REQUEST_INTERNAL) || 402 (priv_ep->flags & EP_TDLCHK_EN) || 403 priv_ep->use_streams) { 404 dev_dbg(priv_dev->dev, "Blocking external request\n"); 405 return ret; 406 } 407 } 408 409 while (!list_empty(&priv_ep->deferred_req_list)) { 410 request = cdns3_next_request(&priv_ep->deferred_req_list); 411 412 if (!priv_ep->use_streams) { 413 ret = cdns3_ep_run_transfer(priv_ep, request); 414 } else { 415 priv_ep->stream_sg_idx = 0; 416 ret = cdns3_ep_run_stream_transfer(priv_ep, request); 417 } 418 if (ret) 419 return ret; 420 421 list_move_tail(&request->list, &priv_ep->pending_req_list); 422 if (request->stream_id != 0 || (priv_ep->flags & EP_TDLCHK_EN)) 423 break; 424 } 425 426 priv_ep->flags &= ~EP_RING_FULL; 427 return ret; 428 } 429 430 /* 431 * WA2: Set flag for all not ISOC OUT endpoints. If this flag is set 432 * driver try to detect whether endpoint need additional internal 433 * buffer for unblocking on-chip FIFO buffer. This flag will be cleared 434 * if before first DESCMISS interrupt the DMA will be armed. 435 */ 436 #define cdns3_wa2_enable_detection(priv_dev, priv_ep, reg) do { \ 437 if (!priv_ep->dir && priv_ep->type != USB_ENDPOINT_XFER_ISOC) { \ 438 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_DET; \ 439 (reg) |= EP_STS_EN_DESCMISEN; \ 440 } } while (0) 441 442 static void __cdns3_descmiss_copy_data(struct usb_request *request, 443 struct usb_request *descmiss_req) 444 { 445 int length = request->actual + descmiss_req->actual; 446 struct scatterlist *s = request->sg; 447 448 if (!s) { 449 if (length <= request->length) { 450 memcpy(&((u8 *)request->buf)[request->actual], 451 descmiss_req->buf, 452 descmiss_req->actual); 453 request->actual = length; 454 } else { 455 /* It should never occures */ 456 request->status = -ENOMEM; 457 } 458 } else { 459 if (length <= sg_dma_len(s)) { 460 void *p = phys_to_virt(sg_dma_address(s)); 461 462 memcpy(&((u8 *)p)[request->actual], 463 descmiss_req->buf, 464 descmiss_req->actual); 465 request->actual = length; 466 } else { 467 request->status = -ENOMEM; 468 } 469 } 470 } 471 472 /** 473 * cdns3_wa2_descmiss_copy_data - copy data from internal requests to 474 * request queued by class driver. 475 * @priv_ep: extended endpoint object 476 * @request: request object 477 */ 478 static void cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint *priv_ep, 479 struct usb_request *request) 480 { 481 struct usb_request *descmiss_req; 482 struct cdns3_request *descmiss_priv_req; 483 484 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) { 485 int chunk_end; 486 487 descmiss_priv_req = 488 cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list); 489 descmiss_req = &descmiss_priv_req->request; 490 491 /* driver can't touch pending request */ 492 if (descmiss_priv_req->flags & REQUEST_PENDING) 493 break; 494 495 chunk_end = descmiss_priv_req->flags & REQUEST_INTERNAL_CH; 496 request->status = descmiss_req->status; 497 __cdns3_descmiss_copy_data(request, descmiss_req); 498 list_del_init(&descmiss_priv_req->list); 499 kfree(descmiss_req->buf); 500 cdns3_gadget_ep_free_request(&priv_ep->endpoint, descmiss_req); 501 --priv_ep->wa2_counter; 502 503 if (!chunk_end) 504 break; 505 } 506 } 507 508 static struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev, 509 struct cdns3_endpoint *priv_ep, 510 struct cdns3_request *priv_req) 511 { 512 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN && 513 priv_req->flags & REQUEST_INTERNAL) { 514 struct usb_request *req; 515 516 req = cdns3_next_request(&priv_ep->deferred_req_list); 517 518 priv_ep->descmis_req = NULL; 519 520 if (!req) 521 return NULL; 522 523 /* unmap the gadget request before copying data */ 524 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, req, 525 priv_ep->dir); 526 527 cdns3_wa2_descmiss_copy_data(priv_ep, req); 528 if (!(priv_ep->flags & EP_QUIRK_END_TRANSFER) && 529 req->length != req->actual) { 530 /* wait for next part of transfer */ 531 /* re-map the gadget request buffer*/ 532 usb_gadget_map_request_by_dev(priv_dev->sysdev, req, 533 usb_endpoint_dir_in(priv_ep->endpoint.desc)); 534 return NULL; 535 } 536 537 if (req->status == -EINPROGRESS) 538 req->status = 0; 539 540 list_del_init(&req->list); 541 cdns3_start_all_request(priv_dev, priv_ep); 542 return req; 543 } 544 545 return &priv_req->request; 546 } 547 548 static int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev, 549 struct cdns3_endpoint *priv_ep, 550 struct cdns3_request *priv_req) 551 { 552 int deferred = 0; 553 554 /* 555 * If transfer was queued before DESCMISS appear than we 556 * can disable handling of DESCMISS interrupt. Driver assumes that it 557 * can disable special treatment for this endpoint. 558 */ 559 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) { 560 u32 reg; 561 562 cdns3_select_ep(priv_dev, priv_ep->num | priv_ep->dir); 563 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET; 564 reg = readl(&priv_dev->regs->ep_sts_en); 565 reg &= ~EP_STS_EN_DESCMISEN; 566 trace_cdns3_wa2(priv_ep, "workaround disabled\n"); 567 writel(reg, &priv_dev->regs->ep_sts_en); 568 } 569 570 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) { 571 u8 pending_empty = list_empty(&priv_ep->pending_req_list); 572 u8 descmiss_empty = list_empty(&priv_ep->wa2_descmiss_req_list); 573 574 /* 575 * DESCMISS transfer has been finished, so data will be 576 * directly copied from internal allocated usb_request 577 * objects. 578 */ 579 if (pending_empty && !descmiss_empty && 580 !(priv_req->flags & REQUEST_INTERNAL)) { 581 cdns3_wa2_descmiss_copy_data(priv_ep, 582 &priv_req->request); 583 584 trace_cdns3_wa2(priv_ep, "get internal stored data"); 585 586 list_add_tail(&priv_req->request.list, 587 &priv_ep->pending_req_list); 588 cdns3_gadget_giveback(priv_ep, priv_req, 589 priv_req->request.status); 590 591 /* 592 * Intentionally driver returns positive value as 593 * correct value. It informs that transfer has 594 * been finished. 595 */ 596 return EINPROGRESS; 597 } 598 599 /* 600 * Driver will wait for completion DESCMISS transfer, 601 * before starts new, not DESCMISS transfer. 602 */ 603 if (!pending_empty && !descmiss_empty) { 604 trace_cdns3_wa2(priv_ep, "wait for pending transfer\n"); 605 deferred = 1; 606 } 607 608 if (priv_req->flags & REQUEST_INTERNAL) 609 list_add_tail(&priv_req->list, 610 &priv_ep->wa2_descmiss_req_list); 611 } 612 613 return deferred; 614 } 615 616 static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep) 617 { 618 struct cdns3_request *priv_req; 619 620 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) { 621 u8 chain; 622 623 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list); 624 chain = !!(priv_req->flags & REQUEST_INTERNAL_CH); 625 626 trace_cdns3_wa2(priv_ep, "removes eldest request"); 627 628 kfree(priv_req->request.buf); 629 list_del_init(&priv_req->list); 630 cdns3_gadget_ep_free_request(&priv_ep->endpoint, 631 &priv_req->request); 632 --priv_ep->wa2_counter; 633 634 if (!chain) 635 break; 636 } 637 } 638 639 /** 640 * cdns3_wa2_descmissing_packet - handles descriptor missing event. 641 * @priv_ep: extended gadget object 642 * 643 * This function is used only for WA2. For more information see Work around 2 644 * description. 645 */ 646 static void cdns3_wa2_descmissing_packet(struct cdns3_endpoint *priv_ep) 647 { 648 struct cdns3_request *priv_req; 649 struct usb_request *request; 650 u8 pending_empty = list_empty(&priv_ep->pending_req_list); 651 652 /* check for pending transfer */ 653 if (!pending_empty) { 654 trace_cdns3_wa2(priv_ep, "Ignoring Descriptor missing IRQ\n"); 655 return; 656 } 657 658 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) { 659 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET; 660 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_EN; 661 } 662 663 trace_cdns3_wa2(priv_ep, "Description Missing detected\n"); 664 665 if (priv_ep->wa2_counter >= CDNS3_WA2_NUM_BUFFERS) { 666 trace_cdns3_wa2(priv_ep, "WA2 overflow\n"); 667 cdns3_wa2_remove_old_request(priv_ep); 668 } 669 670 request = cdns3_gadget_ep_alloc_request(&priv_ep->endpoint, 671 GFP_ATOMIC); 672 if (!request) 673 goto err; 674 675 priv_req = to_cdns3_request(request); 676 priv_req->flags |= REQUEST_INTERNAL; 677 678 /* if this field is still assigned it indicate that transfer related 679 * with this request has not been finished yet. Driver in this 680 * case simply allocate next request and assign flag REQUEST_INTERNAL_CH 681 * flag to previous one. It will indicate that current request is 682 * part of the previous one. 683 */ 684 if (priv_ep->descmis_req) 685 priv_ep->descmis_req->flags |= REQUEST_INTERNAL_CH; 686 687 priv_req->request.buf = kzalloc(CDNS3_DESCMIS_BUF_SIZE, 688 GFP_ATOMIC); 689 priv_ep->wa2_counter++; 690 691 if (!priv_req->request.buf) { 692 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request); 693 goto err; 694 } 695 696 priv_req->request.length = CDNS3_DESCMIS_BUF_SIZE; 697 priv_ep->descmis_req = priv_req; 698 699 __cdns3_gadget_ep_queue(&priv_ep->endpoint, 700 &priv_ep->descmis_req->request, 701 GFP_ATOMIC); 702 703 return; 704 705 err: 706 dev_err(priv_ep->cdns3_dev->dev, 707 "Failed: No sufficient memory for DESCMIS\n"); 708 } 709 710 static void cdns3_wa2_reset_tdl(struct cdns3_device *priv_dev) 711 { 712 u16 tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd)); 713 714 if (tdl) { 715 u16 reset_val = EP_CMD_TDL_MAX + 1 - tdl; 716 717 writel(EP_CMD_TDL_SET(reset_val) | EP_CMD_STDL, 718 &priv_dev->regs->ep_cmd); 719 } 720 } 721 722 static void cdns3_wa2_check_outq_status(struct cdns3_device *priv_dev) 723 { 724 u32 ep_sts_reg; 725 726 /* select EP0-out */ 727 cdns3_select_ep(priv_dev, 0); 728 729 ep_sts_reg = readl(&priv_dev->regs->ep_sts); 730 731 if (EP_STS_OUTQ_VAL(ep_sts_reg)) { 732 u32 outq_ep_num = EP_STS_OUTQ_NO(ep_sts_reg); 733 struct cdns3_endpoint *outq_ep = priv_dev->eps[outq_ep_num]; 734 735 if ((outq_ep->flags & EP_ENABLED) && !(outq_ep->use_streams) && 736 outq_ep->type != USB_ENDPOINT_XFER_ISOC && outq_ep_num) { 737 u8 pending_empty = list_empty(&outq_ep->pending_req_list); 738 739 if ((outq_ep->flags & EP_QUIRK_EXTRA_BUF_DET) || 740 (outq_ep->flags & EP_QUIRK_EXTRA_BUF_EN) || 741 !pending_empty) { 742 } else { 743 u32 ep_sts_en_reg; 744 u32 ep_cmd_reg; 745 746 cdns3_select_ep(priv_dev, outq_ep->num | 747 outq_ep->dir); 748 ep_sts_en_reg = readl(&priv_dev->regs->ep_sts_en); 749 ep_cmd_reg = readl(&priv_dev->regs->ep_cmd); 750 751 outq_ep->flags |= EP_TDLCHK_EN; 752 cdns3_set_register_bit(&priv_dev->regs->ep_cfg, 753 EP_CFG_TDL_CHK); 754 755 cdns3_wa2_enable_detection(priv_dev, outq_ep, 756 ep_sts_en_reg); 757 writel(ep_sts_en_reg, 758 &priv_dev->regs->ep_sts_en); 759 /* reset tdl value to zero */ 760 cdns3_wa2_reset_tdl(priv_dev); 761 /* 762 * Memory barrier - Reset tdl before ringing the 763 * doorbell. 764 */ 765 wmb(); 766 if (EP_CMD_DRDY & ep_cmd_reg) { 767 trace_cdns3_wa2(outq_ep, "Enabling WA2 skipping doorbell\n"); 768 769 } else { 770 trace_cdns3_wa2(outq_ep, "Enabling WA2 ringing doorbell\n"); 771 /* 772 * ring doorbell to generate DESCMIS irq 773 */ 774 writel(EP_CMD_DRDY, 775 &priv_dev->regs->ep_cmd); 776 } 777 } 778 } 779 } 780 } 781 782 /** 783 * cdns3_gadget_giveback - call struct usb_request's ->complete callback 784 * @priv_ep: The endpoint to whom the request belongs to 785 * @priv_req: The request we're giving back 786 * @status: completion code for the request 787 * 788 * Must be called with controller's lock held and interrupts disabled. This 789 * function will unmap @req and call its ->complete() callback to notify upper 790 * layers that it has completed. 791 */ 792 void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep, 793 struct cdns3_request *priv_req, 794 int status) 795 { 796 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 797 struct usb_request *request = &priv_req->request; 798 799 list_del_init(&request->list); 800 801 if (request->status == -EINPROGRESS) 802 request->status = status; 803 804 if (likely(!(priv_req->flags & REQUEST_UNALIGNED))) 805 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, request, 806 priv_ep->dir); 807 808 if ((priv_req->flags & REQUEST_UNALIGNED) && 809 priv_ep->dir == USB_DIR_OUT && !request->status) { 810 /* Make DMA buffer CPU accessible */ 811 dma_sync_single_for_cpu(priv_dev->sysdev, 812 priv_req->aligned_buf->dma, 813 request->actual, 814 priv_req->aligned_buf->dir); 815 memcpy(request->buf, priv_req->aligned_buf->buf, 816 request->actual); 817 } 818 819 priv_req->flags &= ~(REQUEST_PENDING | REQUEST_UNALIGNED); 820 /* All TRBs have finished, clear the counter */ 821 priv_req->finished_trb = 0; 822 trace_cdns3_gadget_giveback(priv_req); 823 824 if (priv_dev->dev_ver < DEV_VER_V2) { 825 request = cdns3_wa2_gadget_giveback(priv_dev, priv_ep, 826 priv_req); 827 if (!request) 828 return; 829 } 830 831 if (request->complete) { 832 spin_unlock(&priv_dev->lock); 833 usb_gadget_giveback_request(&priv_ep->endpoint, 834 request); 835 spin_lock(&priv_dev->lock); 836 } 837 838 if (request->buf == priv_dev->zlp_buf) 839 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request); 840 } 841 842 static void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep) 843 { 844 /* Work around for stale data address in TRB*/ 845 if (priv_ep->wa1_set) { 846 trace_cdns3_wa1(priv_ep, "restore cycle bit"); 847 848 priv_ep->wa1_set = 0; 849 priv_ep->wa1_trb_index = 0xFFFF; 850 if (priv_ep->wa1_cycle_bit) { 851 priv_ep->wa1_trb->control = 852 priv_ep->wa1_trb->control | cpu_to_le32(0x1); 853 } else { 854 priv_ep->wa1_trb->control = 855 priv_ep->wa1_trb->control & cpu_to_le32(~0x1); 856 } 857 } 858 } 859 860 static void cdns3_free_aligned_request_buf(struct work_struct *work) 861 { 862 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device, 863 aligned_buf_wq); 864 struct cdns3_aligned_buf *buf, *tmp; 865 unsigned long flags; 866 867 spin_lock_irqsave(&priv_dev->lock, flags); 868 869 list_for_each_entry_safe(buf, tmp, &priv_dev->aligned_buf_list, list) { 870 if (!buf->in_use) { 871 list_del(&buf->list); 872 873 /* 874 * Re-enable interrupts to free DMA capable memory. 875 * Driver can't free this memory with disabled 876 * interrupts. 877 */ 878 spin_unlock_irqrestore(&priv_dev->lock, flags); 879 dma_free_noncoherent(priv_dev->sysdev, buf->size, 880 buf->buf, buf->dma, buf->dir); 881 kfree(buf); 882 spin_lock_irqsave(&priv_dev->lock, flags); 883 } 884 } 885 886 spin_unlock_irqrestore(&priv_dev->lock, flags); 887 } 888 889 static int cdns3_prepare_aligned_request_buf(struct cdns3_request *priv_req) 890 { 891 struct cdns3_endpoint *priv_ep = priv_req->priv_ep; 892 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 893 struct cdns3_aligned_buf *buf; 894 895 /* check if buffer is aligned to 8. */ 896 if (!((uintptr_t)priv_req->request.buf & 0x7)) 897 return 0; 898 899 buf = priv_req->aligned_buf; 900 901 if (!buf || priv_req->request.length > buf->size) { 902 buf = kzalloc(sizeof(*buf), GFP_ATOMIC); 903 if (!buf) 904 return -ENOMEM; 905 906 buf->size = priv_req->request.length; 907 buf->dir = usb_endpoint_dir_in(priv_ep->endpoint.desc) ? 908 DMA_TO_DEVICE : DMA_FROM_DEVICE; 909 910 buf->buf = dma_alloc_noncoherent(priv_dev->sysdev, 911 buf->size, 912 &buf->dma, 913 buf->dir, 914 GFP_ATOMIC); 915 if (!buf->buf) { 916 kfree(buf); 917 return -ENOMEM; 918 } 919 920 if (priv_req->aligned_buf) { 921 trace_cdns3_free_aligned_request(priv_req); 922 priv_req->aligned_buf->in_use = 0; 923 queue_work(system_freezable_wq, 924 &priv_dev->aligned_buf_wq); 925 } 926 927 buf->in_use = 1; 928 priv_req->aligned_buf = buf; 929 930 list_add_tail(&buf->list, 931 &priv_dev->aligned_buf_list); 932 } 933 934 if (priv_ep->dir == USB_DIR_IN) { 935 /* Make DMA buffer CPU accessible */ 936 dma_sync_single_for_cpu(priv_dev->sysdev, 937 buf->dma, buf->size, buf->dir); 938 memcpy(buf->buf, priv_req->request.buf, 939 priv_req->request.length); 940 } 941 942 /* Transfer DMA buffer ownership back to device */ 943 dma_sync_single_for_device(priv_dev->sysdev, 944 buf->dma, buf->size, buf->dir); 945 946 priv_req->flags |= REQUEST_UNALIGNED; 947 trace_cdns3_prepare_aligned_request(priv_req); 948 949 return 0; 950 } 951 952 static int cdns3_wa1_update_guard(struct cdns3_endpoint *priv_ep, 953 struct cdns3_trb *trb) 954 { 955 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 956 957 if (!priv_ep->wa1_set) { 958 u32 doorbell; 959 960 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY); 961 962 if (doorbell) { 963 priv_ep->wa1_cycle_bit = priv_ep->pcs ? TRB_CYCLE : 0; 964 priv_ep->wa1_set = 1; 965 priv_ep->wa1_trb = trb; 966 priv_ep->wa1_trb_index = priv_ep->enqueue; 967 trace_cdns3_wa1(priv_ep, "set guard"); 968 return 0; 969 } 970 } 971 return 1; 972 } 973 974 static void cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device *priv_dev, 975 struct cdns3_endpoint *priv_ep) 976 { 977 int dma_index; 978 u32 doorbell; 979 980 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY); 981 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep); 982 983 if (!doorbell || dma_index != priv_ep->wa1_trb_index) 984 cdns3_wa1_restore_cycle_bit(priv_ep); 985 } 986 987 static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep, 988 struct usb_request *request) 989 { 990 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 991 struct cdns3_request *priv_req; 992 struct cdns3_trb *trb; 993 dma_addr_t trb_dma; 994 int address; 995 u32 control; 996 u32 length; 997 u32 tdl; 998 unsigned int sg_idx = priv_ep->stream_sg_idx; 999 1000 priv_req = to_cdns3_request(request); 1001 address = priv_ep->endpoint.desc->bEndpointAddress; 1002 1003 priv_ep->flags |= EP_PENDING_REQUEST; 1004 1005 /* must allocate buffer aligned to 8 */ 1006 if (priv_req->flags & REQUEST_UNALIGNED) 1007 trb_dma = priv_req->aligned_buf->dma; 1008 else 1009 trb_dma = request->dma; 1010 1011 /* For stream capable endpoints driver use only single TD. */ 1012 trb = priv_ep->trb_pool + priv_ep->enqueue; 1013 priv_req->start_trb = priv_ep->enqueue; 1014 priv_req->end_trb = priv_req->start_trb; 1015 priv_req->trb = trb; 1016 1017 cdns3_select_ep(priv_ep->cdns3_dev, address); 1018 1019 control = TRB_TYPE(TRB_NORMAL) | TRB_CYCLE | 1020 TRB_STREAM_ID(priv_req->request.stream_id) | TRB_ISP; 1021 1022 if (!request->num_sgs) { 1023 trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma)); 1024 length = request->length; 1025 } else { 1026 trb->buffer = cpu_to_le32(TRB_BUFFER(request->sg[sg_idx].dma_address)); 1027 length = request->sg[sg_idx].length; 1028 } 1029 1030 tdl = DIV_ROUND_UP(length, priv_ep->endpoint.maxpacket); 1031 1032 trb->length = cpu_to_le32(TRB_BURST_LEN(16) | TRB_LEN(length)); 1033 1034 /* 1035 * For DEV_VER_V2 controller version we have enabled 1036 * USB_CONF2_EN_TDL_TRB in DMULT configuration. 1037 * This enables TDL calculation based on TRB, hence setting TDL in TRB. 1038 */ 1039 if (priv_dev->dev_ver >= DEV_VER_V2) { 1040 if (priv_dev->gadget.speed == USB_SPEED_SUPER) 1041 trb->length |= cpu_to_le32(TRB_TDL_SS_SIZE(tdl)); 1042 } 1043 priv_req->flags |= REQUEST_PENDING; 1044 1045 trb->control = cpu_to_le32(control); 1046 1047 trace_cdns3_prepare_trb(priv_ep, priv_req->trb); 1048 1049 /* 1050 * Memory barrier - Cycle Bit must be set before trb->length and 1051 * trb->buffer fields. 1052 */ 1053 wmb(); 1054 1055 /* always first element */ 1056 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma), 1057 &priv_dev->regs->ep_traddr); 1058 1059 if (!(priv_ep->flags & EP_STALLED)) { 1060 trace_cdns3_ring(priv_ep); 1061 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/ 1062 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts); 1063 1064 priv_ep->prime_flag = false; 1065 1066 /* 1067 * Controller version DEV_VER_V2 tdl calculation 1068 * is based on TRB 1069 */ 1070 1071 if (priv_dev->dev_ver < DEV_VER_V2) 1072 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, 1073 &priv_dev->regs->ep_cmd); 1074 else if (priv_dev->dev_ver > DEV_VER_V2) 1075 writel(tdl, &priv_dev->regs->ep_tdl); 1076 1077 priv_ep->last_stream_id = priv_req->request.stream_id; 1078 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd); 1079 writel(EP_CMD_ERDY_SID(priv_req->request.stream_id) | 1080 EP_CMD_ERDY, &priv_dev->regs->ep_cmd); 1081 1082 trace_cdns3_doorbell_epx(priv_ep->name, 1083 readl(&priv_dev->regs->ep_traddr)); 1084 } 1085 1086 /* WORKAROUND for transition to L0 */ 1087 __cdns3_gadget_wakeup(priv_dev); 1088 1089 return 0; 1090 } 1091 1092 static void cdns3_rearm_drdy_if_needed(struct cdns3_endpoint *priv_ep) 1093 { 1094 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 1095 1096 if (priv_dev->dev_ver < DEV_VER_V3) 1097 return; 1098 1099 if (readl(&priv_dev->regs->ep_sts) & EP_STS_TRBERR) { 1100 writel(EP_STS_TRBERR, &priv_dev->regs->ep_sts); 1101 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd); 1102 } 1103 } 1104 1105 /** 1106 * cdns3_ep_run_transfer - start transfer on no-default endpoint hardware 1107 * @priv_ep: endpoint object 1108 * @request: request object 1109 * 1110 * Returns zero on success or negative value on failure 1111 */ 1112 static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep, 1113 struct usb_request *request) 1114 { 1115 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 1116 struct cdns3_request *priv_req; 1117 struct cdns3_trb *trb; 1118 struct cdns3_trb *link_trb = NULL; 1119 dma_addr_t trb_dma; 1120 u32 togle_pcs = 1; 1121 int sg_iter = 0; 1122 int num_trb_req; 1123 int trb_burst; 1124 int num_trb; 1125 int address; 1126 u32 control; 1127 int pcs; 1128 u16 total_tdl = 0; 1129 struct scatterlist *s = NULL; 1130 bool sg_supported = !!(request->num_mapped_sgs); 1131 1132 num_trb_req = sg_supported ? request->num_mapped_sgs : 1; 1133 1134 /* ISO transfer require each SOF have a TD, each TD include some TRBs */ 1135 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) 1136 num_trb = priv_ep->interval * num_trb_req; 1137 else 1138 num_trb = num_trb_req; 1139 1140 priv_req = to_cdns3_request(request); 1141 address = priv_ep->endpoint.desc->bEndpointAddress; 1142 1143 priv_ep->flags |= EP_PENDING_REQUEST; 1144 1145 /* must allocate buffer aligned to 8 */ 1146 if (priv_req->flags & REQUEST_UNALIGNED) 1147 trb_dma = priv_req->aligned_buf->dma; 1148 else 1149 trb_dma = request->dma; 1150 1151 trb = priv_ep->trb_pool + priv_ep->enqueue; 1152 priv_req->start_trb = priv_ep->enqueue; 1153 priv_req->trb = trb; 1154 1155 cdns3_select_ep(priv_ep->cdns3_dev, address); 1156 1157 /* prepare ring */ 1158 if ((priv_ep->enqueue + num_trb) >= (priv_ep->num_trbs - 1)) { 1159 int doorbell, dma_index; 1160 u32 ch_bit = 0; 1161 1162 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY); 1163 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep); 1164 1165 /* Driver can't update LINK TRB if it is current processed. */ 1166 if (doorbell && dma_index == priv_ep->num_trbs - 1) { 1167 priv_ep->flags |= EP_DEFERRED_DRDY; 1168 return -ENOBUFS; 1169 } 1170 1171 /*updating C bt in Link TRB before starting DMA*/ 1172 link_trb = priv_ep->trb_pool + (priv_ep->num_trbs - 1); 1173 /* 1174 * For TRs size equal 2 enabling TRB_CHAIN for epXin causes 1175 * that DMA stuck at the LINK TRB. 1176 * On the other hand, removing TRB_CHAIN for longer TRs for 1177 * epXout cause that DMA stuck after handling LINK TRB. 1178 * To eliminate this strange behavioral driver set TRB_CHAIN 1179 * bit only for TR size > 2. 1180 */ 1181 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC || 1182 TRBS_PER_SEGMENT > 2) 1183 ch_bit = TRB_CHAIN; 1184 1185 link_trb->control = cpu_to_le32(((priv_ep->pcs) ? TRB_CYCLE : 0) | 1186 TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit); 1187 1188 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) { 1189 /* 1190 * ISO require LINK TRB must be first one of TD. 1191 * Fill LINK TRBs for left trb space to simply software process logic. 1192 */ 1193 while (priv_ep->enqueue) { 1194 *trb = *link_trb; 1195 trace_cdns3_prepare_trb(priv_ep, trb); 1196 1197 cdns3_ep_inc_enq(priv_ep); 1198 trb = priv_ep->trb_pool + priv_ep->enqueue; 1199 priv_req->trb = trb; 1200 } 1201 } 1202 } 1203 1204 if (num_trb > priv_ep->free_trbs) { 1205 priv_ep->flags |= EP_RING_FULL; 1206 return -ENOBUFS; 1207 } 1208 1209 if (priv_dev->dev_ver <= DEV_VER_V2) 1210 togle_pcs = cdns3_wa1_update_guard(priv_ep, trb); 1211 1212 /* set incorrect Cycle Bit for first trb*/ 1213 control = priv_ep->pcs ? 0 : TRB_CYCLE; 1214 trb->length = 0; 1215 if (priv_dev->dev_ver >= DEV_VER_V2) { 1216 u16 td_size; 1217 1218 td_size = DIV_ROUND_UP(request->length, 1219 priv_ep->endpoint.maxpacket); 1220 if (priv_dev->gadget.speed == USB_SPEED_SUPER) 1221 trb->length = cpu_to_le32(TRB_TDL_SS_SIZE(td_size)); 1222 else 1223 control |= TRB_TDL_HS_SIZE(td_size); 1224 } 1225 1226 do { 1227 u32 length; 1228 1229 if (!(sg_iter % num_trb_req) && sg_supported) 1230 s = request->sg; 1231 1232 /* fill TRB */ 1233 control |= TRB_TYPE(TRB_NORMAL); 1234 if (sg_supported) { 1235 trb->buffer = cpu_to_le32(TRB_BUFFER(sg_dma_address(s))); 1236 length = sg_dma_len(s); 1237 } else { 1238 trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma)); 1239 length = request->length; 1240 } 1241 1242 if (priv_ep->flags & EP_TDLCHK_EN) 1243 total_tdl += DIV_ROUND_UP(length, 1244 priv_ep->endpoint.maxpacket); 1245 1246 trb_burst = priv_ep->trb_burst_size; 1247 1248 /* 1249 * Supposed DMA cross 4k bounder problem should be fixed at DEV_VER_V2, but still 1250 * met problem when do ISO transfer if sg enabled. 1251 * 1252 * Data pattern likes below when sg enabled, package size is 1k and mult is 2 1253 * [UVC Header(8B) ] [data(3k - 8)] ... 1254 * 1255 * The received data at offset 0xd000 will get 0xc000 data, len 0x70. Error happen 1256 * as below pattern: 1257 * 0xd000: wrong 1258 * 0xe000: wrong 1259 * 0xf000: correct 1260 * 0x10000: wrong 1261 * 0x11000: wrong 1262 * 0x12000: correct 1263 * ... 1264 * 1265 * But it is still unclear about why error have not happen below 0xd000, it should 1266 * cross 4k bounder. But anyway, the below code can fix this problem. 1267 * 1268 * To avoid DMA cross 4k bounder at ISO transfer, reduce burst len according to 16. 1269 */ 1270 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && priv_dev->dev_ver <= DEV_VER_V2) 1271 if (ALIGN_DOWN(trb->buffer, SZ_4K) != 1272 ALIGN_DOWN(trb->buffer + length, SZ_4K)) 1273 trb_burst = 16; 1274 1275 trb->length |= cpu_to_le32(TRB_BURST_LEN(trb_burst) | 1276 TRB_LEN(length)); 1277 pcs = priv_ep->pcs ? TRB_CYCLE : 0; 1278 1279 /* 1280 * first trb should be prepared as last to avoid processing 1281 * transfer to early 1282 */ 1283 if (sg_iter != 0) 1284 control |= pcs; 1285 1286 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) { 1287 control |= TRB_IOC | TRB_ISP; 1288 } else { 1289 /* for last element in TD or in SG list */ 1290 if (sg_iter == (num_trb - 1) && sg_iter != 0) 1291 control |= pcs | TRB_IOC | TRB_ISP; 1292 } 1293 1294 if (sg_iter) 1295 trb->control = cpu_to_le32(control); 1296 else 1297 priv_req->trb->control = cpu_to_le32(control); 1298 1299 if (sg_supported) { 1300 trb->control |= cpu_to_le32(TRB_ISP); 1301 /* Don't set chain bit for last TRB */ 1302 if ((sg_iter % num_trb_req) < num_trb_req - 1) 1303 trb->control |= cpu_to_le32(TRB_CHAIN); 1304 1305 s = sg_next(s); 1306 } 1307 1308 control = 0; 1309 ++sg_iter; 1310 priv_req->end_trb = priv_ep->enqueue; 1311 cdns3_ep_inc_enq(priv_ep); 1312 trb = priv_ep->trb_pool + priv_ep->enqueue; 1313 trb->length = 0; 1314 } while (sg_iter < num_trb); 1315 1316 trb = priv_req->trb; 1317 1318 priv_req->flags |= REQUEST_PENDING; 1319 priv_req->num_of_trb = num_trb; 1320 1321 if (sg_iter == 1) 1322 trb->control |= cpu_to_le32(TRB_IOC | TRB_ISP); 1323 1324 if (priv_dev->dev_ver < DEV_VER_V2 && 1325 (priv_ep->flags & EP_TDLCHK_EN)) { 1326 u16 tdl = total_tdl; 1327 u16 old_tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd)); 1328 1329 if (tdl > EP_CMD_TDL_MAX) { 1330 tdl = EP_CMD_TDL_MAX; 1331 priv_ep->pending_tdl = total_tdl - EP_CMD_TDL_MAX; 1332 } 1333 1334 if (old_tdl < tdl) { 1335 tdl -= old_tdl; 1336 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, 1337 &priv_dev->regs->ep_cmd); 1338 } 1339 } 1340 1341 /* 1342 * Memory barrier - cycle bit must be set before other filds in trb. 1343 */ 1344 wmb(); 1345 1346 /* give the TD to the consumer*/ 1347 if (togle_pcs) 1348 trb->control = trb->control ^ cpu_to_le32(1); 1349 1350 if (priv_dev->dev_ver <= DEV_VER_V2) 1351 cdns3_wa1_tray_restore_cycle_bit(priv_dev, priv_ep); 1352 1353 if (num_trb > 1) { 1354 int i = 0; 1355 1356 while (i < num_trb) { 1357 trace_cdns3_prepare_trb(priv_ep, trb + i); 1358 if (trb + i == link_trb) { 1359 trb = priv_ep->trb_pool; 1360 num_trb = num_trb - i; 1361 i = 0; 1362 } else { 1363 i++; 1364 } 1365 } 1366 } else { 1367 trace_cdns3_prepare_trb(priv_ep, priv_req->trb); 1368 } 1369 1370 /* 1371 * Memory barrier - Cycle Bit must be set before trb->length and 1372 * trb->buffer fields. 1373 */ 1374 wmb(); 1375 1376 /* 1377 * For DMULT mode we can set address to transfer ring only once after 1378 * enabling endpoint. 1379 */ 1380 if (priv_ep->flags & EP_UPDATE_EP_TRBADDR) { 1381 /* 1382 * Until SW is not ready to handle the OUT transfer the ISO OUT 1383 * Endpoint should be disabled (EP_CFG.ENABLE = 0). 1384 * EP_CFG_ENABLE must be set before updating ep_traddr. 1385 */ 1386 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir && 1387 !(priv_ep->flags & EP_QUIRK_ISO_OUT_EN)) { 1388 priv_ep->flags |= EP_QUIRK_ISO_OUT_EN; 1389 cdns3_set_register_bit(&priv_dev->regs->ep_cfg, 1390 EP_CFG_ENABLE); 1391 } 1392 1393 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma + 1394 priv_req->start_trb * TRB_SIZE), 1395 &priv_dev->regs->ep_traddr); 1396 1397 priv_ep->flags &= ~EP_UPDATE_EP_TRBADDR; 1398 } 1399 1400 if (!priv_ep->wa1_set && !(priv_ep->flags & EP_STALLED)) { 1401 trace_cdns3_ring(priv_ep); 1402 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/ 1403 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts); 1404 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd); 1405 cdns3_rearm_drdy_if_needed(priv_ep); 1406 trace_cdns3_doorbell_epx(priv_ep->name, 1407 readl(&priv_dev->regs->ep_traddr)); 1408 } 1409 1410 /* WORKAROUND for transition to L0 */ 1411 __cdns3_gadget_wakeup(priv_dev); 1412 1413 return 0; 1414 } 1415 1416 void cdns3_set_hw_configuration(struct cdns3_device *priv_dev) 1417 { 1418 struct cdns3_endpoint *priv_ep; 1419 struct usb_ep *ep; 1420 1421 if (priv_dev->hw_configured_flag) 1422 return; 1423 1424 writel(USB_CONF_CFGSET, &priv_dev->regs->usb_conf); 1425 1426 cdns3_set_register_bit(&priv_dev->regs->usb_conf, 1427 USB_CONF_U1EN | USB_CONF_U2EN); 1428 1429 priv_dev->hw_configured_flag = 1; 1430 1431 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) { 1432 if (ep->enabled) { 1433 priv_ep = ep_to_cdns3_ep(ep); 1434 cdns3_start_all_request(priv_dev, priv_ep); 1435 } 1436 } 1437 1438 cdns3_allow_enable_l1(priv_dev, 1); 1439 } 1440 1441 /** 1442 * cdns3_trb_handled - check whether trb has been handled by DMA 1443 * 1444 * @priv_ep: extended endpoint object. 1445 * @priv_req: request object for checking 1446 * 1447 * Endpoint must be selected before invoking this function. 1448 * 1449 * Returns false if request has not been handled by DMA, else returns true. 1450 * 1451 * SR - start ring 1452 * ER - end ring 1453 * DQ = priv_ep->dequeue - dequeue position 1454 * EQ = priv_ep->enqueue - enqueue position 1455 * ST = priv_req->start_trb - index of first TRB in transfer ring 1456 * ET = priv_req->end_trb - index of last TRB in transfer ring 1457 * CI = current_index - index of processed TRB by DMA. 1458 * 1459 * As first step, we check if the TRB between the ST and ET. 1460 * Then, we check if cycle bit for index priv_ep->dequeue 1461 * is correct. 1462 * 1463 * some rules: 1464 * 1. priv_ep->dequeue never equals to current_index. 1465 * 2 priv_ep->enqueue never exceed priv_ep->dequeue 1466 * 3. exception: priv_ep->enqueue == priv_ep->dequeue 1467 * and priv_ep->free_trbs is zero. 1468 * This case indicate that TR is full. 1469 * 1470 * At below two cases, the request have been handled. 1471 * Case 1 - priv_ep->dequeue < current_index 1472 * SR ... EQ ... DQ ... CI ... ER 1473 * SR ... DQ ... CI ... EQ ... ER 1474 * 1475 * Case 2 - priv_ep->dequeue > current_index 1476 * This situation takes place when CI go through the LINK TRB at the end of 1477 * transfer ring. 1478 * SR ... CI ... EQ ... DQ ... ER 1479 */ 1480 static bool cdns3_trb_handled(struct cdns3_endpoint *priv_ep, 1481 struct cdns3_request *priv_req) 1482 { 1483 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 1484 struct cdns3_trb *trb; 1485 int current_index = 0; 1486 int handled = 0; 1487 int doorbell; 1488 1489 current_index = cdns3_get_dma_pos(priv_dev, priv_ep); 1490 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY); 1491 1492 /* current trb doesn't belong to this request */ 1493 if (priv_req->start_trb < priv_req->end_trb) { 1494 if (priv_ep->dequeue > priv_req->end_trb) 1495 goto finish; 1496 1497 if (priv_ep->dequeue < priv_req->start_trb) 1498 goto finish; 1499 } 1500 1501 if ((priv_req->start_trb > priv_req->end_trb) && 1502 (priv_ep->dequeue > priv_req->end_trb) && 1503 (priv_ep->dequeue < priv_req->start_trb)) 1504 goto finish; 1505 1506 if ((priv_req->start_trb == priv_req->end_trb) && 1507 (priv_ep->dequeue != priv_req->end_trb)) 1508 goto finish; 1509 1510 trb = &priv_ep->trb_pool[priv_ep->dequeue]; 1511 1512 if ((le32_to_cpu(trb->control) & TRB_CYCLE) != priv_ep->ccs) 1513 goto finish; 1514 1515 if (doorbell == 1 && current_index == priv_ep->dequeue) 1516 goto finish; 1517 1518 /* The corner case for TRBS_PER_SEGMENT equal 2). */ 1519 if (TRBS_PER_SEGMENT == 2 && priv_ep->type != USB_ENDPOINT_XFER_ISOC) { 1520 handled = 1; 1521 goto finish; 1522 } 1523 1524 if (priv_ep->enqueue == priv_ep->dequeue && 1525 priv_ep->free_trbs == 0) { 1526 handled = 1; 1527 } else if (priv_ep->dequeue < current_index) { 1528 if ((current_index == (priv_ep->num_trbs - 1)) && 1529 !priv_ep->dequeue) 1530 goto finish; 1531 1532 handled = 1; 1533 } else if (priv_ep->dequeue > current_index) { 1534 handled = 1; 1535 } 1536 1537 finish: 1538 trace_cdns3_request_handled(priv_req, current_index, handled); 1539 1540 return handled; 1541 } 1542 1543 static void cdns3_transfer_completed(struct cdns3_device *priv_dev, 1544 struct cdns3_endpoint *priv_ep) 1545 { 1546 struct cdns3_request *priv_req; 1547 struct usb_request *request; 1548 struct cdns3_trb *trb; 1549 bool request_handled = false; 1550 bool transfer_end = false; 1551 1552 while (!list_empty(&priv_ep->pending_req_list)) { 1553 request = cdns3_next_request(&priv_ep->pending_req_list); 1554 priv_req = to_cdns3_request(request); 1555 1556 trb = priv_ep->trb_pool + priv_ep->dequeue; 1557 1558 /* The TRB was changed as link TRB, and the request was handled at ep_dequeue */ 1559 while (TRB_FIELD_TO_TYPE(le32_to_cpu(trb->control)) == TRB_LINK) { 1560 1561 /* ISO ep_traddr may stop at LINK TRB */ 1562 if (priv_ep->dequeue == cdns3_get_dma_pos(priv_dev, priv_ep) && 1563 priv_ep->type == USB_ENDPOINT_XFER_ISOC) 1564 break; 1565 1566 trace_cdns3_complete_trb(priv_ep, trb); 1567 cdns3_ep_inc_deq(priv_ep); 1568 trb = priv_ep->trb_pool + priv_ep->dequeue; 1569 } 1570 1571 if (!request->stream_id) { 1572 /* Re-select endpoint. It could be changed by other CPU 1573 * during handling usb_gadget_giveback_request. 1574 */ 1575 cdns3_select_ep(priv_dev, priv_ep->endpoint.address); 1576 1577 while (cdns3_trb_handled(priv_ep, priv_req)) { 1578 priv_req->finished_trb++; 1579 if (priv_req->finished_trb >= priv_req->num_of_trb) 1580 request_handled = true; 1581 1582 trb = priv_ep->trb_pool + priv_ep->dequeue; 1583 trace_cdns3_complete_trb(priv_ep, trb); 1584 1585 if (!transfer_end) 1586 request->actual += 1587 TRB_LEN(le32_to_cpu(trb->length)); 1588 1589 if (priv_req->num_of_trb > 1 && 1590 le32_to_cpu(trb->control) & TRB_SMM && 1591 le32_to_cpu(trb->control) & TRB_CHAIN) 1592 transfer_end = true; 1593 1594 cdns3_ep_inc_deq(priv_ep); 1595 } 1596 1597 if (request_handled) { 1598 /* TRBs are duplicated by priv_ep->interval time for ISO IN */ 1599 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && priv_ep->dir) 1600 request->actual /= priv_ep->interval; 1601 1602 cdns3_gadget_giveback(priv_ep, priv_req, 0); 1603 request_handled = false; 1604 transfer_end = false; 1605 } else { 1606 goto prepare_next_td; 1607 } 1608 1609 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC && 1610 TRBS_PER_SEGMENT == 2) 1611 break; 1612 } else { 1613 /* Re-select endpoint. It could be changed by other CPU 1614 * during handling usb_gadget_giveback_request. 1615 */ 1616 cdns3_select_ep(priv_dev, priv_ep->endpoint.address); 1617 1618 trb = priv_ep->trb_pool; 1619 trace_cdns3_complete_trb(priv_ep, trb); 1620 1621 if (trb != priv_req->trb) 1622 dev_warn(priv_dev->dev, 1623 "request_trb=0x%p, queue_trb=0x%p\n", 1624 priv_req->trb, trb); 1625 1626 request->actual += TRB_LEN(le32_to_cpu(trb->length)); 1627 1628 if (!request->num_sgs || 1629 (request->num_sgs == (priv_ep->stream_sg_idx + 1))) { 1630 priv_ep->stream_sg_idx = 0; 1631 cdns3_gadget_giveback(priv_ep, priv_req, 0); 1632 } else { 1633 priv_ep->stream_sg_idx++; 1634 cdns3_ep_run_stream_transfer(priv_ep, request); 1635 } 1636 break; 1637 } 1638 } 1639 priv_ep->flags &= ~EP_PENDING_REQUEST; 1640 1641 prepare_next_td: 1642 if (!(priv_ep->flags & EP_STALLED) && 1643 !(priv_ep->flags & EP_STALL_PENDING)) 1644 cdns3_start_all_request(priv_dev, priv_ep); 1645 } 1646 1647 void cdns3_rearm_transfer(struct cdns3_endpoint *priv_ep, u8 rearm) 1648 { 1649 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 1650 1651 cdns3_wa1_restore_cycle_bit(priv_ep); 1652 1653 if (rearm) { 1654 trace_cdns3_ring(priv_ep); 1655 1656 /* Cycle Bit must be updated before arming DMA. */ 1657 wmb(); 1658 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd); 1659 1660 __cdns3_gadget_wakeup(priv_dev); 1661 1662 trace_cdns3_doorbell_epx(priv_ep->name, 1663 readl(&priv_dev->regs->ep_traddr)); 1664 } 1665 } 1666 1667 static void cdns3_reprogram_tdl(struct cdns3_endpoint *priv_ep) 1668 { 1669 u16 tdl = priv_ep->pending_tdl; 1670 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 1671 1672 if (tdl > EP_CMD_TDL_MAX) { 1673 tdl = EP_CMD_TDL_MAX; 1674 priv_ep->pending_tdl -= EP_CMD_TDL_MAX; 1675 } else { 1676 priv_ep->pending_tdl = 0; 1677 } 1678 1679 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, &priv_dev->regs->ep_cmd); 1680 } 1681 1682 /** 1683 * cdns3_check_ep_interrupt_proceed - Processes interrupt related to endpoint 1684 * @priv_ep: endpoint object 1685 * 1686 * Returns 0 1687 */ 1688 static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep) 1689 { 1690 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 1691 u32 ep_sts_reg; 1692 struct usb_request *deferred_request; 1693 struct usb_request *pending_request; 1694 u32 tdl = 0; 1695 1696 cdns3_select_ep(priv_dev, priv_ep->endpoint.address); 1697 1698 trace_cdns3_epx_irq(priv_dev, priv_ep); 1699 1700 ep_sts_reg = readl(&priv_dev->regs->ep_sts); 1701 writel(ep_sts_reg, &priv_dev->regs->ep_sts); 1702 1703 if ((ep_sts_reg & EP_STS_PRIME) && priv_ep->use_streams) { 1704 bool dbusy = !!(ep_sts_reg & EP_STS_DBUSY); 1705 1706 tdl = cdns3_get_tdl(priv_dev); 1707 1708 /* 1709 * Continue the previous transfer: 1710 * There is some racing between ERDY and PRIME. The device send 1711 * ERDY and almost in the same time Host send PRIME. It cause 1712 * that host ignore the ERDY packet and driver has to send it 1713 * again. 1714 */ 1715 if (tdl && (dbusy || !EP_STS_BUFFEMPTY(ep_sts_reg) || 1716 EP_STS_HOSTPP(ep_sts_reg))) { 1717 writel(EP_CMD_ERDY | 1718 EP_CMD_ERDY_SID(priv_ep->last_stream_id), 1719 &priv_dev->regs->ep_cmd); 1720 ep_sts_reg &= ~(EP_STS_MD_EXIT | EP_STS_IOC); 1721 } else { 1722 priv_ep->prime_flag = true; 1723 1724 pending_request = cdns3_next_request(&priv_ep->pending_req_list); 1725 deferred_request = cdns3_next_request(&priv_ep->deferred_req_list); 1726 1727 if (deferred_request && !pending_request) { 1728 cdns3_start_all_request(priv_dev, priv_ep); 1729 } 1730 } 1731 } 1732 1733 if (ep_sts_reg & EP_STS_TRBERR) { 1734 if (priv_ep->flags & EP_STALL_PENDING && 1735 !(ep_sts_reg & EP_STS_DESCMIS && 1736 priv_dev->dev_ver < DEV_VER_V2)) { 1737 cdns3_ep_stall_flush(priv_ep); 1738 } 1739 1740 /* 1741 * For isochronous transfer driver completes request on 1742 * IOC or on TRBERR. IOC appears only when device receive 1743 * OUT data packet. If host disable stream or lost some packet 1744 * then the only way to finish all queued transfer is to do it 1745 * on TRBERR event. 1746 */ 1747 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && 1748 !priv_ep->wa1_set) { 1749 if (!priv_ep->dir) { 1750 u32 ep_cfg = readl(&priv_dev->regs->ep_cfg); 1751 1752 ep_cfg &= ~EP_CFG_ENABLE; 1753 writel(ep_cfg, &priv_dev->regs->ep_cfg); 1754 priv_ep->flags &= ~EP_QUIRK_ISO_OUT_EN; 1755 priv_ep->flags |= EP_UPDATE_EP_TRBADDR; 1756 } 1757 cdns3_transfer_completed(priv_dev, priv_ep); 1758 } else if (!(priv_ep->flags & EP_STALLED) && 1759 !(priv_ep->flags & EP_STALL_PENDING)) { 1760 if (priv_ep->flags & EP_DEFERRED_DRDY) { 1761 priv_ep->flags &= ~EP_DEFERRED_DRDY; 1762 cdns3_start_all_request(priv_dev, priv_ep); 1763 } else { 1764 cdns3_rearm_transfer(priv_ep, 1765 priv_ep->wa1_set); 1766 } 1767 } 1768 } 1769 1770 if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP) || 1771 (ep_sts_reg & EP_STS_IOT)) { 1772 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) { 1773 if (ep_sts_reg & EP_STS_ISP) 1774 priv_ep->flags |= EP_QUIRK_END_TRANSFER; 1775 else 1776 priv_ep->flags &= ~EP_QUIRK_END_TRANSFER; 1777 } 1778 1779 if (!priv_ep->use_streams) { 1780 if ((ep_sts_reg & EP_STS_IOC) || 1781 (ep_sts_reg & EP_STS_ISP)) { 1782 cdns3_transfer_completed(priv_dev, priv_ep); 1783 } else if ((priv_ep->flags & EP_TDLCHK_EN) & 1784 priv_ep->pending_tdl) { 1785 /* handle IOT with pending tdl */ 1786 cdns3_reprogram_tdl(priv_ep); 1787 } 1788 } else if (priv_ep->dir == USB_DIR_OUT) { 1789 priv_ep->ep_sts_pending |= ep_sts_reg; 1790 } else if (ep_sts_reg & EP_STS_IOT) { 1791 cdns3_transfer_completed(priv_dev, priv_ep); 1792 } 1793 } 1794 1795 /* 1796 * MD_EXIT interrupt sets when stream capable endpoint exits 1797 * from MOVE DATA state of Bulk IN/OUT stream protocol state machine 1798 */ 1799 if (priv_ep->dir == USB_DIR_OUT && (ep_sts_reg & EP_STS_MD_EXIT) && 1800 (priv_ep->ep_sts_pending & EP_STS_IOT) && priv_ep->use_streams) { 1801 priv_ep->ep_sts_pending = 0; 1802 cdns3_transfer_completed(priv_dev, priv_ep); 1803 } 1804 1805 /* 1806 * WA2: this condition should only be meet when 1807 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET or 1808 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN. 1809 * In other cases this interrupt will be disabled. 1810 */ 1811 if (ep_sts_reg & EP_STS_DESCMIS && priv_dev->dev_ver < DEV_VER_V2 && 1812 !(priv_ep->flags & EP_STALLED)) 1813 cdns3_wa2_descmissing_packet(priv_ep); 1814 1815 return 0; 1816 } 1817 1818 static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev) 1819 { 1820 if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect) 1821 priv_dev->gadget_driver->disconnect(&priv_dev->gadget); 1822 } 1823 1824 /** 1825 * cdns3_check_usb_interrupt_proceed - Processes interrupt related to device 1826 * @priv_dev: extended gadget object 1827 * @usb_ists: bitmap representation of device's reported interrupts 1828 * (usb_ists register value) 1829 */ 1830 static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev, 1831 u32 usb_ists) 1832 __must_hold(&priv_dev->lock) 1833 { 1834 int speed = 0; 1835 1836 trace_cdns3_usb_irq(priv_dev, usb_ists); 1837 if (usb_ists & USB_ISTS_L1ENTI) { 1838 /* 1839 * WORKAROUND: CDNS3 controller has issue with hardware resuming 1840 * from L1. To fix it, if any DMA transfer is pending driver 1841 * must starts driving resume signal immediately. 1842 */ 1843 if (readl(&priv_dev->regs->drbl)) 1844 __cdns3_gadget_wakeup(priv_dev); 1845 } 1846 1847 /* Connection detected */ 1848 if (usb_ists & (USB_ISTS_CON2I | USB_ISTS_CONI)) { 1849 speed = cdns3_get_speed(priv_dev); 1850 priv_dev->gadget.speed = speed; 1851 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_POWERED); 1852 cdns3_ep0_config(priv_dev); 1853 } 1854 1855 /* Disconnection detected */ 1856 if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) { 1857 spin_unlock(&priv_dev->lock); 1858 cdns3_disconnect_gadget(priv_dev); 1859 spin_lock(&priv_dev->lock); 1860 priv_dev->gadget.speed = USB_SPEED_UNKNOWN; 1861 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED); 1862 cdns3_hw_reset_eps_config(priv_dev); 1863 } 1864 1865 if (usb_ists & (USB_ISTS_L2ENTI | USB_ISTS_U3ENTI)) { 1866 if (priv_dev->gadget_driver && 1867 priv_dev->gadget_driver->suspend) { 1868 spin_unlock(&priv_dev->lock); 1869 priv_dev->gadget_driver->suspend(&priv_dev->gadget); 1870 spin_lock(&priv_dev->lock); 1871 } 1872 } 1873 1874 if (usb_ists & (USB_ISTS_L2EXTI | USB_ISTS_U3EXTI)) { 1875 if (priv_dev->gadget_driver && 1876 priv_dev->gadget_driver->resume) { 1877 spin_unlock(&priv_dev->lock); 1878 priv_dev->gadget_driver->resume(&priv_dev->gadget); 1879 spin_lock(&priv_dev->lock); 1880 } 1881 } 1882 1883 /* reset*/ 1884 if (usb_ists & (USB_ISTS_UWRESI | USB_ISTS_UHRESI | USB_ISTS_U2RESI)) { 1885 if (priv_dev->gadget_driver) { 1886 spin_unlock(&priv_dev->lock); 1887 usb_gadget_udc_reset(&priv_dev->gadget, 1888 priv_dev->gadget_driver); 1889 spin_lock(&priv_dev->lock); 1890 1891 /*read again to check the actual speed*/ 1892 speed = cdns3_get_speed(priv_dev); 1893 priv_dev->gadget.speed = speed; 1894 cdns3_hw_reset_eps_config(priv_dev); 1895 cdns3_ep0_config(priv_dev); 1896 } 1897 } 1898 } 1899 1900 /** 1901 * cdns3_device_irq_handler - interrupt handler for device part of controller 1902 * 1903 * @irq: irq number for cdns3 core device 1904 * @data: structure of cdns3 1905 * 1906 * Returns IRQ_HANDLED or IRQ_NONE 1907 */ 1908 static irqreturn_t cdns3_device_irq_handler(int irq, void *data) 1909 { 1910 struct cdns3_device *priv_dev = data; 1911 struct cdns *cdns = dev_get_drvdata(priv_dev->dev); 1912 irqreturn_t ret = IRQ_NONE; 1913 u32 reg; 1914 1915 if (cdns->in_lpm) 1916 return ret; 1917 1918 /* check USB device interrupt */ 1919 reg = readl(&priv_dev->regs->usb_ists); 1920 if (reg) { 1921 /* After masking interrupts the new interrupts won't be 1922 * reported in usb_ists/ep_ists. In order to not lose some 1923 * of them driver disables only detected interrupts. 1924 * They will be enabled ASAP after clearing source of 1925 * interrupt. This an unusual behavior only applies to 1926 * usb_ists register. 1927 */ 1928 reg = ~reg & readl(&priv_dev->regs->usb_ien); 1929 /* mask deferred interrupt. */ 1930 writel(reg, &priv_dev->regs->usb_ien); 1931 ret = IRQ_WAKE_THREAD; 1932 } 1933 1934 /* check endpoint interrupt */ 1935 reg = readl(&priv_dev->regs->ep_ists); 1936 if (reg) { 1937 writel(0, &priv_dev->regs->ep_ien); 1938 ret = IRQ_WAKE_THREAD; 1939 } 1940 1941 return ret; 1942 } 1943 1944 /** 1945 * cdns3_device_thread_irq_handler - interrupt handler for device part 1946 * of controller 1947 * 1948 * @irq: irq number for cdns3 core device 1949 * @data: structure of cdns3 1950 * 1951 * Returns IRQ_HANDLED or IRQ_NONE 1952 */ 1953 static irqreturn_t cdns3_device_thread_irq_handler(int irq, void *data) 1954 { 1955 struct cdns3_device *priv_dev = data; 1956 irqreturn_t ret = IRQ_NONE; 1957 unsigned long flags; 1958 unsigned int bit; 1959 unsigned long reg; 1960 1961 spin_lock_irqsave(&priv_dev->lock, flags); 1962 1963 reg = readl(&priv_dev->regs->usb_ists); 1964 if (reg) { 1965 writel(reg, &priv_dev->regs->usb_ists); 1966 writel(USB_IEN_INIT, &priv_dev->regs->usb_ien); 1967 cdns3_check_usb_interrupt_proceed(priv_dev, reg); 1968 ret = IRQ_HANDLED; 1969 } 1970 1971 reg = readl(&priv_dev->regs->ep_ists); 1972 1973 /* handle default endpoint OUT */ 1974 if (reg & EP_ISTS_EP_OUT0) { 1975 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT); 1976 ret = IRQ_HANDLED; 1977 } 1978 1979 /* handle default endpoint IN */ 1980 if (reg & EP_ISTS_EP_IN0) { 1981 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN); 1982 ret = IRQ_HANDLED; 1983 } 1984 1985 /* check if interrupt from non default endpoint, if no exit */ 1986 reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0); 1987 if (!reg) 1988 goto irqend; 1989 1990 for_each_set_bit(bit, ®, 1991 sizeof(u32) * BITS_PER_BYTE) { 1992 cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]); 1993 ret = IRQ_HANDLED; 1994 } 1995 1996 if (priv_dev->dev_ver < DEV_VER_V2 && priv_dev->using_streams) 1997 cdns3_wa2_check_outq_status(priv_dev); 1998 1999 irqend: 2000 writel(~0, &priv_dev->regs->ep_ien); 2001 spin_unlock_irqrestore(&priv_dev->lock, flags); 2002 2003 return ret; 2004 } 2005 2006 /** 2007 * cdns3_ep_onchip_buffer_reserve - Try to reserve onchip buf for EP 2008 * 2009 * The real reservation will occur during write to EP_CFG register, 2010 * this function is used to check if the 'size' reservation is allowed. 2011 * 2012 * @priv_dev: extended gadget object 2013 * @size: the size (KB) for EP would like to allocate 2014 * @is_in: endpoint direction 2015 * 2016 * Return 0 if the required size can met or negative value on failure 2017 */ 2018 static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev, 2019 int size, int is_in) 2020 { 2021 int remained; 2022 2023 /* 2KB are reserved for EP0*/ 2024 remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2; 2025 2026 if (is_in) { 2027 if (remained < size) 2028 return -EPERM; 2029 2030 priv_dev->onchip_used_size += size; 2031 } else { 2032 int required; 2033 2034 /** 2035 * ALL OUT EPs are shared the same chunk onchip memory, so 2036 * driver checks if it already has assigned enough buffers 2037 */ 2038 if (priv_dev->out_mem_is_allocated >= size) 2039 return 0; 2040 2041 required = size - priv_dev->out_mem_is_allocated; 2042 2043 if (required > remained) 2044 return -EPERM; 2045 2046 priv_dev->out_mem_is_allocated += required; 2047 priv_dev->onchip_used_size += required; 2048 } 2049 2050 return 0; 2051 } 2052 2053 static void cdns3_configure_dmult(struct cdns3_device *priv_dev, 2054 struct cdns3_endpoint *priv_ep) 2055 { 2056 struct cdns3_usb_regs __iomem *regs = priv_dev->regs; 2057 2058 /* For dev_ver > DEV_VER_V2 DMULT is configured per endpoint */ 2059 if (priv_dev->dev_ver <= DEV_VER_V2) 2060 writel(USB_CONF_DMULT, ®s->usb_conf); 2061 2062 if (priv_dev->dev_ver == DEV_VER_V2) 2063 writel(USB_CONF2_EN_TDL_TRB, ®s->usb_conf2); 2064 2065 if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) { 2066 u32 mask; 2067 2068 if (priv_ep->dir) 2069 mask = BIT(priv_ep->num + 16); 2070 else 2071 mask = BIT(priv_ep->num); 2072 2073 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) { 2074 cdns3_set_register_bit(®s->tdl_from_trb, mask); 2075 cdns3_set_register_bit(®s->tdl_beh, mask); 2076 cdns3_set_register_bit(®s->tdl_beh2, mask); 2077 cdns3_set_register_bit(®s->dma_adv_td, mask); 2078 } 2079 2080 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) 2081 cdns3_set_register_bit(®s->tdl_from_trb, mask); 2082 2083 cdns3_set_register_bit(®s->dtrans, mask); 2084 } 2085 } 2086 2087 /** 2088 * cdns3_ep_config - Configure hardware endpoint 2089 * @priv_ep: extended endpoint object 2090 * @enable: set EP_CFG_ENABLE bit in ep_cfg register. 2091 */ 2092 int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable) 2093 { 2094 bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC); 2095 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 2096 u32 bEndpointAddress = priv_ep->num | priv_ep->dir; 2097 u32 max_packet_size = priv_ep->wMaxPacketSize; 2098 u8 maxburst = priv_ep->bMaxBurst; 2099 u32 ep_cfg = 0; 2100 u8 buffering; 2101 int ret; 2102 2103 buffering = priv_dev->ep_buf_size - 1; 2104 2105 cdns3_configure_dmult(priv_dev, priv_ep); 2106 2107 switch (priv_ep->type) { 2108 case USB_ENDPOINT_XFER_INT: 2109 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT); 2110 2111 if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir) 2112 ep_cfg |= EP_CFG_TDL_CHK; 2113 break; 2114 case USB_ENDPOINT_XFER_BULK: 2115 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK); 2116 2117 if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir) 2118 ep_cfg |= EP_CFG_TDL_CHK; 2119 break; 2120 default: 2121 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC); 2122 buffering = (priv_ep->bMaxBurst + 1) * (priv_ep->mult + 1) - 1; 2123 } 2124 2125 switch (priv_dev->gadget.speed) { 2126 case USB_SPEED_FULL: 2127 max_packet_size = is_iso_ep ? 1023 : 64; 2128 break; 2129 case USB_SPEED_HIGH: 2130 max_packet_size = is_iso_ep ? 1024 : 512; 2131 break; 2132 case USB_SPEED_SUPER: 2133 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC) { 2134 max_packet_size = 1024; 2135 maxburst = priv_dev->ep_buf_size - 1; 2136 } 2137 break; 2138 default: 2139 /* all other speed are not supported */ 2140 return -EINVAL; 2141 } 2142 2143 if (max_packet_size == 1024) 2144 priv_ep->trb_burst_size = 128; 2145 else if (max_packet_size >= 512) 2146 priv_ep->trb_burst_size = 64; 2147 else 2148 priv_ep->trb_burst_size = 16; 2149 2150 /* 2151 * In versions preceding DEV_VER_V2, for example, iMX8QM, there exit the bugs 2152 * in the DMA. These bugs occur when the trb_burst_size exceeds 16 and the 2153 * address is not aligned to 128 Bytes (which is a product of the 64-bit AXI 2154 * and AXI maximum burst length of 16 or 0xF+1, dma_axi_ctrl0[3:0]). This 2155 * results in data corruption when it crosses the 4K border. The corruption 2156 * specifically occurs from the position (4K - (address & 0x7F)) to 4K. 2157 * 2158 * So force trb_burst_size to 16 at such platform. 2159 */ 2160 if (priv_dev->dev_ver < DEV_VER_V2) 2161 priv_ep->trb_burst_size = 16; 2162 2163 buffering = min_t(u8, buffering, EP_CFG_BUFFERING_MAX); 2164 maxburst = min_t(u8, maxburst, EP_CFG_MAXBURST_MAX); 2165 2166 /* onchip buffer is only allocated before configuration */ 2167 if (!priv_dev->hw_configured_flag) { 2168 ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1, 2169 !!priv_ep->dir); 2170 if (ret) { 2171 dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n"); 2172 return ret; 2173 } 2174 } 2175 2176 if (enable) 2177 ep_cfg |= EP_CFG_ENABLE; 2178 2179 if (priv_ep->use_streams && priv_dev->gadget.speed >= USB_SPEED_SUPER) { 2180 if (priv_dev->dev_ver >= DEV_VER_V3) { 2181 u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0)); 2182 2183 /* 2184 * Stream capable endpoints are handled by using ep_tdl 2185 * register. Other endpoints use TDL from TRB feature. 2186 */ 2187 cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb, 2188 mask); 2189 } 2190 2191 /* Enable Stream Bit TDL chk and SID chk */ 2192 ep_cfg |= EP_CFG_STREAM_EN | EP_CFG_TDL_CHK | EP_CFG_SID_CHK; 2193 } 2194 2195 ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) | 2196 EP_CFG_MULT(priv_ep->mult) | /* must match EP setting */ 2197 EP_CFG_BUFFERING(buffering) | 2198 EP_CFG_MAXBURST(maxburst); 2199 2200 cdns3_select_ep(priv_dev, bEndpointAddress); 2201 writel(ep_cfg, &priv_dev->regs->ep_cfg); 2202 priv_ep->flags |= EP_CONFIGURED; 2203 2204 dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n", 2205 priv_ep->name, ep_cfg); 2206 2207 return 0; 2208 } 2209 2210 /* Find correct direction for HW endpoint according to description */ 2211 static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc, 2212 struct cdns3_endpoint *priv_ep) 2213 { 2214 return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) || 2215 (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc)); 2216 } 2217 2218 static struct 2219 cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev, 2220 struct usb_endpoint_descriptor *desc) 2221 { 2222 struct usb_ep *ep; 2223 struct cdns3_endpoint *priv_ep; 2224 2225 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) { 2226 unsigned long num; 2227 int ret; 2228 /* ep name pattern likes epXin or epXout */ 2229 char c[2] = {ep->name[2], '\0'}; 2230 2231 ret = kstrtoul(c, 10, &num); 2232 if (ret) 2233 return ERR_PTR(ret); 2234 2235 priv_ep = ep_to_cdns3_ep(ep); 2236 if (cdns3_ep_dir_is_correct(desc, priv_ep)) { 2237 if (!(priv_ep->flags & EP_CLAIMED)) { 2238 priv_ep->num = num; 2239 return priv_ep; 2240 } 2241 } 2242 } 2243 2244 return ERR_PTR(-ENOENT); 2245 } 2246 2247 /* 2248 * Cadence IP has one limitation that all endpoints must be configured 2249 * (Type & MaxPacketSize) before setting configuration through hardware 2250 * register, it means we can't change endpoints configuration after 2251 * set_configuration. 2252 * 2253 * This function set EP_CLAIMED flag which is added when the gadget driver 2254 * uses usb_ep_autoconfig to configure specific endpoint; 2255 * When the udc driver receives set_configurion request, 2256 * it goes through all claimed endpoints, and configure all endpoints 2257 * accordingly. 2258 * 2259 * At usb_ep_ops.enable/disable, we only enable and disable endpoint through 2260 * ep_cfg register which can be changed after set_configuration, and do 2261 * some software operation accordingly. 2262 */ 2263 static struct 2264 usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget, 2265 struct usb_endpoint_descriptor *desc, 2266 struct usb_ss_ep_comp_descriptor *comp_desc) 2267 { 2268 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget); 2269 struct cdns3_endpoint *priv_ep; 2270 unsigned long flags; 2271 2272 priv_ep = cdns3_find_available_ep(priv_dev, desc); 2273 if (IS_ERR(priv_ep)) { 2274 dev_err(priv_dev->dev, "no available ep\n"); 2275 return NULL; 2276 } 2277 2278 dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name); 2279 2280 spin_lock_irqsave(&priv_dev->lock, flags); 2281 priv_ep->endpoint.desc = desc; 2282 priv_ep->dir = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT; 2283 priv_ep->type = usb_endpoint_type(desc); 2284 priv_ep->flags |= EP_CLAIMED; 2285 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0; 2286 priv_ep->wMaxPacketSize = usb_endpoint_maxp(desc); 2287 priv_ep->mult = USB_EP_MAXP_MULT(priv_ep->wMaxPacketSize); 2288 priv_ep->wMaxPacketSize &= USB_ENDPOINT_MAXP_MASK; 2289 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && comp_desc) { 2290 priv_ep->mult = USB_SS_MULT(comp_desc->bmAttributes) - 1; 2291 priv_ep->bMaxBurst = comp_desc->bMaxBurst; 2292 } 2293 2294 spin_unlock_irqrestore(&priv_dev->lock, flags); 2295 return &priv_ep->endpoint; 2296 } 2297 2298 /** 2299 * cdns3_gadget_ep_alloc_request - Allocates request 2300 * @ep: endpoint object associated with request 2301 * @gfp_flags: gfp flags 2302 * 2303 * Returns allocated request address, NULL on allocation error 2304 */ 2305 struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep, 2306 gfp_t gfp_flags) 2307 { 2308 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep); 2309 struct cdns3_request *priv_req; 2310 2311 priv_req = kzalloc(sizeof(*priv_req), gfp_flags); 2312 if (!priv_req) 2313 return NULL; 2314 2315 priv_req->priv_ep = priv_ep; 2316 2317 trace_cdns3_alloc_request(priv_req); 2318 return &priv_req->request; 2319 } 2320 2321 /** 2322 * cdns3_gadget_ep_free_request - Free memory occupied by request 2323 * @ep: endpoint object associated with request 2324 * @request: request to free memory 2325 */ 2326 void cdns3_gadget_ep_free_request(struct usb_ep *ep, 2327 struct usb_request *request) 2328 { 2329 struct cdns3_request *priv_req = to_cdns3_request(request); 2330 2331 if (priv_req->aligned_buf) 2332 priv_req->aligned_buf->in_use = 0; 2333 2334 trace_cdns3_free_request(priv_req); 2335 kfree(priv_req); 2336 } 2337 2338 /** 2339 * cdns3_gadget_ep_enable - Enable endpoint 2340 * @ep: endpoint object 2341 * @desc: endpoint descriptor 2342 * 2343 * Returns 0 on success, error code elsewhere 2344 */ 2345 static int cdns3_gadget_ep_enable(struct usb_ep *ep, 2346 const struct usb_endpoint_descriptor *desc) 2347 { 2348 struct cdns3_endpoint *priv_ep; 2349 struct cdns3_device *priv_dev; 2350 const struct usb_ss_ep_comp_descriptor *comp_desc; 2351 u32 reg = EP_STS_EN_TRBERREN; 2352 u32 bEndpointAddress; 2353 unsigned long flags; 2354 int enable = 1; 2355 int ret = 0; 2356 int val; 2357 2358 if (!ep) { 2359 pr_debug("usbss: ep not configured?\n"); 2360 return -EINVAL; 2361 } 2362 2363 priv_ep = ep_to_cdns3_ep(ep); 2364 priv_dev = priv_ep->cdns3_dev; 2365 comp_desc = priv_ep->endpoint.comp_desc; 2366 2367 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) { 2368 dev_dbg(priv_dev->dev, "usbss: invalid parameters\n"); 2369 return -EINVAL; 2370 } 2371 2372 if (!desc->wMaxPacketSize) { 2373 dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n"); 2374 return -EINVAL; 2375 } 2376 2377 if (dev_WARN_ONCE(priv_dev->dev, priv_ep->flags & EP_ENABLED, 2378 "%s is already enabled\n", priv_ep->name)) 2379 return 0; 2380 2381 spin_lock_irqsave(&priv_dev->lock, flags); 2382 2383 priv_ep->endpoint.desc = desc; 2384 priv_ep->type = usb_endpoint_type(desc); 2385 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0; 2386 2387 if (priv_ep->interval > ISO_MAX_INTERVAL && 2388 priv_ep->type == USB_ENDPOINT_XFER_ISOC) { 2389 dev_err(priv_dev->dev, "Driver is limited to %d period\n", 2390 ISO_MAX_INTERVAL); 2391 2392 ret = -EINVAL; 2393 goto exit; 2394 } 2395 2396 bEndpointAddress = priv_ep->num | priv_ep->dir; 2397 cdns3_select_ep(priv_dev, bEndpointAddress); 2398 2399 /* 2400 * For some versions of controller at some point during ISO OUT traffic 2401 * DMA reads Transfer Ring for the EP which has never got doorbell. 2402 * This issue was detected only on simulation, but to avoid this issue 2403 * driver add protection against it. To fix it driver enable ISO OUT 2404 * endpoint before setting DRBL. This special treatment of ISO OUT 2405 * endpoints are recommended by controller specification. 2406 */ 2407 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) 2408 enable = 0; 2409 2410 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { 2411 /* 2412 * Enable stream support (SS mode) related interrupts 2413 * in EP_STS_EN Register 2414 */ 2415 if (priv_dev->gadget.speed >= USB_SPEED_SUPER) { 2416 reg |= EP_STS_EN_IOTEN | EP_STS_EN_PRIMEEEN | 2417 EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN | 2418 EP_STS_EN_STREAMREN; 2419 priv_ep->use_streams = true; 2420 ret = cdns3_ep_config(priv_ep, enable); 2421 priv_dev->using_streams |= true; 2422 } 2423 } else { 2424 ret = cdns3_ep_config(priv_ep, enable); 2425 } 2426 2427 if (ret) 2428 goto exit; 2429 2430 ret = cdns3_allocate_trb_pool(priv_ep); 2431 if (ret) 2432 goto exit; 2433 2434 bEndpointAddress = priv_ep->num | priv_ep->dir; 2435 cdns3_select_ep(priv_dev, bEndpointAddress); 2436 2437 trace_cdns3_gadget_ep_enable(priv_ep); 2438 2439 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd); 2440 2441 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val, 2442 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)), 2443 1, 1000); 2444 2445 if (unlikely(ret)) { 2446 cdns3_free_trb_pool(priv_ep); 2447 ret = -EINVAL; 2448 goto exit; 2449 } 2450 2451 /* enable interrupt for selected endpoint */ 2452 cdns3_set_register_bit(&priv_dev->regs->ep_ien, 2453 BIT(cdns3_ep_addr_to_index(bEndpointAddress))); 2454 2455 if (priv_dev->dev_ver < DEV_VER_V2) 2456 cdns3_wa2_enable_detection(priv_dev, priv_ep, reg); 2457 2458 writel(reg, &priv_dev->regs->ep_sts_en); 2459 2460 ep->desc = desc; 2461 priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING | 2462 EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN); 2463 priv_ep->flags |= EP_ENABLED | EP_UPDATE_EP_TRBADDR; 2464 priv_ep->wa1_set = 0; 2465 priv_ep->enqueue = 0; 2466 priv_ep->dequeue = 0; 2467 reg = readl(&priv_dev->regs->ep_sts); 2468 priv_ep->pcs = !!EP_STS_CCS(reg); 2469 priv_ep->ccs = !!EP_STS_CCS(reg); 2470 /* one TRB is reserved for link TRB used in DMULT mode*/ 2471 priv_ep->free_trbs = priv_ep->num_trbs - 1; 2472 exit: 2473 spin_unlock_irqrestore(&priv_dev->lock, flags); 2474 2475 return ret; 2476 } 2477 2478 /** 2479 * cdns3_gadget_ep_disable - Disable endpoint 2480 * @ep: endpoint object 2481 * 2482 * Returns 0 on success, error code elsewhere 2483 */ 2484 static int cdns3_gadget_ep_disable(struct usb_ep *ep) 2485 { 2486 struct cdns3_endpoint *priv_ep; 2487 struct cdns3_request *priv_req; 2488 struct cdns3_device *priv_dev; 2489 struct usb_request *request; 2490 unsigned long flags; 2491 int ret = 0; 2492 u32 ep_cfg; 2493 int val; 2494 2495 if (!ep) { 2496 pr_err("usbss: invalid parameters\n"); 2497 return -EINVAL; 2498 } 2499 2500 priv_ep = ep_to_cdns3_ep(ep); 2501 priv_dev = priv_ep->cdns3_dev; 2502 2503 if (dev_WARN_ONCE(priv_dev->dev, !(priv_ep->flags & EP_ENABLED), 2504 "%s is already disabled\n", priv_ep->name)) 2505 return 0; 2506 2507 spin_lock_irqsave(&priv_dev->lock, flags); 2508 2509 trace_cdns3_gadget_ep_disable(priv_ep); 2510 2511 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress); 2512 2513 ep_cfg = readl(&priv_dev->regs->ep_cfg); 2514 ep_cfg &= ~EP_CFG_ENABLE; 2515 writel(ep_cfg, &priv_dev->regs->ep_cfg); 2516 2517 /** 2518 * Driver needs some time before resetting endpoint. 2519 * It need waits for clearing DBUSY bit or for timeout expired. 2520 * 10us is enough time for controller to stop transfer. 2521 */ 2522 readl_poll_timeout_atomic(&priv_dev->regs->ep_sts, val, 2523 !(val & EP_STS_DBUSY), 1, 10); 2524 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd); 2525 2526 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val, 2527 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)), 2528 1, 1000); 2529 if (unlikely(ret)) 2530 dev_err(priv_dev->dev, "Timeout: %s resetting failed.\n", 2531 priv_ep->name); 2532 2533 while (!list_empty(&priv_ep->pending_req_list)) { 2534 request = cdns3_next_request(&priv_ep->pending_req_list); 2535 2536 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 2537 -ESHUTDOWN); 2538 } 2539 2540 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) { 2541 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list); 2542 2543 kfree(priv_req->request.buf); 2544 cdns3_gadget_ep_free_request(&priv_ep->endpoint, 2545 &priv_req->request); 2546 list_del_init(&priv_req->list); 2547 --priv_ep->wa2_counter; 2548 } 2549 2550 while (!list_empty(&priv_ep->deferred_req_list)) { 2551 request = cdns3_next_request(&priv_ep->deferred_req_list); 2552 2553 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 2554 -ESHUTDOWN); 2555 } 2556 2557 priv_ep->descmis_req = NULL; 2558 2559 ep->desc = NULL; 2560 priv_ep->flags &= ~EP_ENABLED; 2561 priv_ep->use_streams = false; 2562 2563 spin_unlock_irqrestore(&priv_dev->lock, flags); 2564 2565 return ret; 2566 } 2567 2568 /** 2569 * __cdns3_gadget_ep_queue - Transfer data on endpoint 2570 * @ep: endpoint object 2571 * @request: request object 2572 * @gfp_flags: gfp flags 2573 * 2574 * Returns 0 on success, error code elsewhere 2575 */ 2576 static int __cdns3_gadget_ep_queue(struct usb_ep *ep, 2577 struct usb_request *request, 2578 gfp_t gfp_flags) 2579 { 2580 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep); 2581 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 2582 struct cdns3_request *priv_req; 2583 int ret = 0; 2584 2585 request->actual = 0; 2586 request->status = -EINPROGRESS; 2587 priv_req = to_cdns3_request(request); 2588 trace_cdns3_ep_queue(priv_req); 2589 2590 if (priv_dev->dev_ver < DEV_VER_V2) { 2591 ret = cdns3_wa2_gadget_ep_queue(priv_dev, priv_ep, 2592 priv_req); 2593 2594 if (ret == EINPROGRESS) 2595 return 0; 2596 } 2597 2598 ret = cdns3_prepare_aligned_request_buf(priv_req); 2599 if (ret < 0) 2600 return ret; 2601 2602 if (likely(!(priv_req->flags & REQUEST_UNALIGNED))) { 2603 ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request, 2604 usb_endpoint_dir_in(ep->desc)); 2605 if (ret) 2606 return ret; 2607 } 2608 2609 list_add_tail(&request->list, &priv_ep->deferred_req_list); 2610 2611 /* 2612 * For stream capable endpoint if prime irq flag is set then only start 2613 * request. 2614 * If hardware endpoint configuration has not been set yet then 2615 * just queue request in deferred list. Transfer will be started in 2616 * cdns3_set_hw_configuration. 2617 */ 2618 if (!request->stream_id) { 2619 if (priv_dev->hw_configured_flag && 2620 !(priv_ep->flags & EP_STALLED) && 2621 !(priv_ep->flags & EP_STALL_PENDING)) 2622 cdns3_start_all_request(priv_dev, priv_ep); 2623 } else { 2624 if (priv_dev->hw_configured_flag && priv_ep->prime_flag) 2625 cdns3_start_all_request(priv_dev, priv_ep); 2626 } 2627 2628 return 0; 2629 } 2630 2631 static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, 2632 gfp_t gfp_flags) 2633 { 2634 struct usb_request *zlp_request; 2635 struct cdns3_endpoint *priv_ep; 2636 struct cdns3_device *priv_dev; 2637 unsigned long flags; 2638 int ret; 2639 2640 if (!request || !ep) 2641 return -EINVAL; 2642 2643 priv_ep = ep_to_cdns3_ep(ep); 2644 priv_dev = priv_ep->cdns3_dev; 2645 2646 spin_lock_irqsave(&priv_dev->lock, flags); 2647 2648 ret = __cdns3_gadget_ep_queue(ep, request, gfp_flags); 2649 2650 if (ret == 0 && request->zero && request->length && 2651 (request->length % ep->maxpacket == 0)) { 2652 struct cdns3_request *priv_req; 2653 2654 zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC); 2655 zlp_request->buf = priv_dev->zlp_buf; 2656 zlp_request->length = 0; 2657 2658 priv_req = to_cdns3_request(zlp_request); 2659 priv_req->flags |= REQUEST_ZLP; 2660 2661 dev_dbg(priv_dev->dev, "Queuing ZLP for endpoint: %s\n", 2662 priv_ep->name); 2663 ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags); 2664 } 2665 2666 spin_unlock_irqrestore(&priv_dev->lock, flags); 2667 return ret; 2668 } 2669 2670 /** 2671 * cdns3_gadget_ep_dequeue - Remove request from transfer queue 2672 * @ep: endpoint object associated with request 2673 * @request: request object 2674 * 2675 * Returns 0 on success, error code elsewhere 2676 */ 2677 int cdns3_gadget_ep_dequeue(struct usb_ep *ep, 2678 struct usb_request *request) 2679 { 2680 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep); 2681 struct cdns3_device *priv_dev; 2682 struct usb_request *req, *req_temp; 2683 struct cdns3_request *priv_req; 2684 struct cdns3_trb *link_trb; 2685 u8 req_on_hw_ring = 0; 2686 unsigned long flags; 2687 int ret = 0; 2688 int val; 2689 2690 if (!ep || !request || !ep->desc) 2691 return -EINVAL; 2692 2693 priv_dev = priv_ep->cdns3_dev; 2694 2695 spin_lock_irqsave(&priv_dev->lock, flags); 2696 2697 priv_req = to_cdns3_request(request); 2698 2699 trace_cdns3_ep_dequeue(priv_req); 2700 2701 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress); 2702 2703 list_for_each_entry_safe(req, req_temp, &priv_ep->pending_req_list, 2704 list) { 2705 if (request == req) { 2706 req_on_hw_ring = 1; 2707 goto found; 2708 } 2709 } 2710 2711 list_for_each_entry_safe(req, req_temp, &priv_ep->deferred_req_list, 2712 list) { 2713 if (request == req) 2714 goto found; 2715 } 2716 2717 goto not_found; 2718 2719 found: 2720 link_trb = priv_req->trb; 2721 2722 /* Update ring only if removed request is on pending_req_list list */ 2723 if (req_on_hw_ring && link_trb) { 2724 /* Stop DMA */ 2725 writel(EP_CMD_DFLUSH, &priv_dev->regs->ep_cmd); 2726 2727 /* wait for DFLUSH cleared */ 2728 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val, 2729 !(val & EP_CMD_DFLUSH), 1, 1000); 2730 2731 link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma + 2732 ((priv_req->end_trb + 1) * TRB_SIZE))); 2733 link_trb->control = cpu_to_le32((le32_to_cpu(link_trb->control) & TRB_CYCLE) | 2734 TRB_TYPE(TRB_LINK) | TRB_CHAIN); 2735 2736 if (priv_ep->wa1_trb == priv_req->trb) 2737 cdns3_wa1_restore_cycle_bit(priv_ep); 2738 } 2739 2740 cdns3_gadget_giveback(priv_ep, priv_req, -ECONNRESET); 2741 2742 req = cdns3_next_request(&priv_ep->pending_req_list); 2743 if (req) 2744 cdns3_rearm_transfer(priv_ep, 1); 2745 2746 not_found: 2747 spin_unlock_irqrestore(&priv_dev->lock, flags); 2748 return ret; 2749 } 2750 2751 /** 2752 * __cdns3_gadget_ep_set_halt - Sets stall on selected endpoint 2753 * Should be called after acquiring spin_lock and selecting ep 2754 * @priv_ep: endpoint object to set stall on. 2755 */ 2756 void __cdns3_gadget_ep_set_halt(struct cdns3_endpoint *priv_ep) 2757 { 2758 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 2759 2760 trace_cdns3_halt(priv_ep, 1, 0); 2761 2762 if (!(priv_ep->flags & EP_STALLED)) { 2763 u32 ep_sts_reg = readl(&priv_dev->regs->ep_sts); 2764 2765 if (!(ep_sts_reg & EP_STS_DBUSY)) 2766 cdns3_ep_stall_flush(priv_ep); 2767 else 2768 priv_ep->flags |= EP_STALL_PENDING; 2769 } 2770 } 2771 2772 /** 2773 * __cdns3_gadget_ep_clear_halt - Clears stall on selected endpoint 2774 * Should be called after acquiring spin_lock and selecting ep 2775 * @priv_ep: endpoint object to clear stall on 2776 */ 2777 int __cdns3_gadget_ep_clear_halt(struct cdns3_endpoint *priv_ep) 2778 { 2779 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 2780 struct usb_request *request; 2781 struct cdns3_request *priv_req; 2782 struct cdns3_trb *trb = NULL; 2783 struct cdns3_trb trb_tmp; 2784 int ret; 2785 int val; 2786 2787 trace_cdns3_halt(priv_ep, 0, 0); 2788 2789 request = cdns3_next_request(&priv_ep->pending_req_list); 2790 if (request) { 2791 priv_req = to_cdns3_request(request); 2792 trb = priv_req->trb; 2793 if (trb) { 2794 trb_tmp = *trb; 2795 trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE); 2796 } 2797 } 2798 2799 writel(EP_CMD_CSTALL | EP_CMD_EPRST, &priv_dev->regs->ep_cmd); 2800 2801 /* wait for EPRST cleared */ 2802 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val, 2803 !(val & EP_CMD_EPRST), 1, 100); 2804 if (ret) 2805 return -EINVAL; 2806 2807 priv_ep->flags &= ~(EP_STALLED | EP_STALL_PENDING); 2808 2809 if (request) { 2810 if (trb) 2811 *trb = trb_tmp; 2812 2813 cdns3_rearm_transfer(priv_ep, 1); 2814 } 2815 2816 cdns3_start_all_request(priv_dev, priv_ep); 2817 return ret; 2818 } 2819 2820 /** 2821 * cdns3_gadget_ep_set_halt - Sets/clears stall on selected endpoint 2822 * @ep: endpoint object to set/clear stall on 2823 * @value: 1 for set stall, 0 for clear stall 2824 * 2825 * Returns 0 on success, error code elsewhere 2826 */ 2827 int cdns3_gadget_ep_set_halt(struct usb_ep *ep, int value) 2828 { 2829 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep); 2830 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 2831 unsigned long flags; 2832 int ret = 0; 2833 2834 if (!(priv_ep->flags & EP_ENABLED)) 2835 return -EPERM; 2836 2837 spin_lock_irqsave(&priv_dev->lock, flags); 2838 2839 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress); 2840 2841 if (!value) { 2842 priv_ep->flags &= ~EP_WEDGE; 2843 ret = __cdns3_gadget_ep_clear_halt(priv_ep); 2844 } else { 2845 __cdns3_gadget_ep_set_halt(priv_ep); 2846 } 2847 2848 spin_unlock_irqrestore(&priv_dev->lock, flags); 2849 2850 return ret; 2851 } 2852 2853 extern const struct usb_ep_ops cdns3_gadget_ep0_ops; 2854 2855 static const struct usb_ep_ops cdns3_gadget_ep_ops = { 2856 .enable = cdns3_gadget_ep_enable, 2857 .disable = cdns3_gadget_ep_disable, 2858 .alloc_request = cdns3_gadget_ep_alloc_request, 2859 .free_request = cdns3_gadget_ep_free_request, 2860 .queue = cdns3_gadget_ep_queue, 2861 .dequeue = cdns3_gadget_ep_dequeue, 2862 .set_halt = cdns3_gadget_ep_set_halt, 2863 .set_wedge = cdns3_gadget_ep_set_wedge, 2864 }; 2865 2866 /** 2867 * cdns3_gadget_get_frame - Returns number of actual ITP frame 2868 * @gadget: gadget object 2869 * 2870 * Returns number of actual ITP frame 2871 */ 2872 static int cdns3_gadget_get_frame(struct usb_gadget *gadget) 2873 { 2874 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget); 2875 2876 return readl(&priv_dev->regs->usb_itpn); 2877 } 2878 2879 int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev) 2880 { 2881 enum usb_device_speed speed; 2882 2883 speed = cdns3_get_speed(priv_dev); 2884 2885 if (speed >= USB_SPEED_SUPER) 2886 return 0; 2887 2888 /* Start driving resume signaling to indicate remote wakeup. */ 2889 writel(USB_CONF_LGO_L0, &priv_dev->regs->usb_conf); 2890 2891 return 0; 2892 } 2893 2894 static int cdns3_gadget_wakeup(struct usb_gadget *gadget) 2895 { 2896 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget); 2897 unsigned long flags; 2898 int ret = 0; 2899 2900 spin_lock_irqsave(&priv_dev->lock, flags); 2901 ret = __cdns3_gadget_wakeup(priv_dev); 2902 spin_unlock_irqrestore(&priv_dev->lock, flags); 2903 return ret; 2904 } 2905 2906 static int cdns3_gadget_set_selfpowered(struct usb_gadget *gadget, 2907 int is_selfpowered) 2908 { 2909 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget); 2910 unsigned long flags; 2911 2912 spin_lock_irqsave(&priv_dev->lock, flags); 2913 priv_dev->is_selfpowered = !!is_selfpowered; 2914 spin_unlock_irqrestore(&priv_dev->lock, flags); 2915 return 0; 2916 } 2917 2918 static int cdns3_gadget_pullup(struct usb_gadget *gadget, int is_on) 2919 { 2920 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget); 2921 2922 if (is_on) { 2923 writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf); 2924 } else { 2925 writel(~0, &priv_dev->regs->ep_ists); 2926 writel(~0, &priv_dev->regs->usb_ists); 2927 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf); 2928 } 2929 2930 return 0; 2931 } 2932 2933 static void cdns3_gadget_config(struct cdns3_device *priv_dev) 2934 { 2935 struct cdns3_usb_regs __iomem *regs = priv_dev->regs; 2936 u32 reg; 2937 2938 cdns3_ep0_config(priv_dev); 2939 2940 /* enable interrupts for endpoint 0 (in and out) */ 2941 writel(EP_IEN_EP_OUT0 | EP_IEN_EP_IN0, ®s->ep_ien); 2942 2943 /* 2944 * Driver needs to modify LFPS minimal U1 Exit time for DEV_VER_TI_V1 2945 * revision of controller. 2946 */ 2947 if (priv_dev->dev_ver == DEV_VER_TI_V1) { 2948 reg = readl(®s->dbg_link1); 2949 2950 reg &= ~DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_MASK; 2951 reg |= DBG_LINK1_LFPS_MIN_GEN_U1_EXIT(0x55) | 2952 DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_SET; 2953 writel(reg, ®s->dbg_link1); 2954 } 2955 2956 /* 2957 * By default some platforms has set protected access to memory. 2958 * This cause problem with cache, so driver restore non-secure 2959 * access to memory. 2960 */ 2961 reg = readl(®s->dma_axi_ctrl); 2962 reg |= DMA_AXI_CTRL_MARPROT(DMA_AXI_CTRL_NON_SECURE) | 2963 DMA_AXI_CTRL_MAWPROT(DMA_AXI_CTRL_NON_SECURE); 2964 writel(reg, ®s->dma_axi_ctrl); 2965 2966 /* enable generic interrupt*/ 2967 writel(USB_IEN_INIT, ®s->usb_ien); 2968 writel(USB_CONF_CLK2OFFDS | USB_CONF_L1DS, ®s->usb_conf); 2969 /* keep Fast Access bit */ 2970 writel(PUSB_PWR_FST_REG_ACCESS, &priv_dev->regs->usb_pwr); 2971 2972 cdns3_configure_dmult(priv_dev, NULL); 2973 } 2974 2975 /** 2976 * cdns3_gadget_udc_start - Gadget start 2977 * @gadget: gadget object 2978 * @driver: driver which operates on this gadget 2979 * 2980 * Returns 0 on success, error code elsewhere 2981 */ 2982 static int cdns3_gadget_udc_start(struct usb_gadget *gadget, 2983 struct usb_gadget_driver *driver) 2984 { 2985 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget); 2986 unsigned long flags; 2987 enum usb_device_speed max_speed = driver->max_speed; 2988 2989 spin_lock_irqsave(&priv_dev->lock, flags); 2990 priv_dev->gadget_driver = driver; 2991 2992 /* limit speed if necessary */ 2993 max_speed = min(driver->max_speed, gadget->max_speed); 2994 2995 switch (max_speed) { 2996 case USB_SPEED_FULL: 2997 writel(USB_CONF_SFORCE_FS, &priv_dev->regs->usb_conf); 2998 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf); 2999 break; 3000 case USB_SPEED_HIGH: 3001 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf); 3002 break; 3003 case USB_SPEED_SUPER: 3004 break; 3005 default: 3006 dev_err(priv_dev->dev, 3007 "invalid maximum_speed parameter %d\n", 3008 max_speed); 3009 fallthrough; 3010 case USB_SPEED_UNKNOWN: 3011 /* default to superspeed */ 3012 max_speed = USB_SPEED_SUPER; 3013 break; 3014 } 3015 3016 cdns3_gadget_config(priv_dev); 3017 spin_unlock_irqrestore(&priv_dev->lock, flags); 3018 return 0; 3019 } 3020 3021 /** 3022 * cdns3_gadget_udc_stop - Stops gadget 3023 * @gadget: gadget object 3024 * 3025 * Returns 0 3026 */ 3027 static int cdns3_gadget_udc_stop(struct usb_gadget *gadget) 3028 { 3029 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget); 3030 struct cdns3_endpoint *priv_ep; 3031 u32 bEndpointAddress; 3032 struct usb_ep *ep; 3033 int val; 3034 3035 priv_dev->gadget_driver = NULL; 3036 3037 priv_dev->onchip_used_size = 0; 3038 priv_dev->out_mem_is_allocated = 0; 3039 priv_dev->gadget.speed = USB_SPEED_UNKNOWN; 3040 3041 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) { 3042 priv_ep = ep_to_cdns3_ep(ep); 3043 bEndpointAddress = priv_ep->num | priv_ep->dir; 3044 cdns3_select_ep(priv_dev, bEndpointAddress); 3045 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd); 3046 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val, 3047 !(val & EP_CMD_EPRST), 1, 100); 3048 3049 priv_ep->flags &= ~EP_CLAIMED; 3050 } 3051 3052 /* disable interrupt for device */ 3053 writel(0, &priv_dev->regs->usb_ien); 3054 writel(0, &priv_dev->regs->usb_pwr); 3055 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf); 3056 3057 return 0; 3058 } 3059 3060 /** 3061 * cdns3_gadget_check_config - ensure cdns3 can support the USB configuration 3062 * @gadget: pointer to the USB gadget 3063 * 3064 * Used to record the maximum number of endpoints being used in a USB composite 3065 * device. (across all configurations) This is to be used in the calculation 3066 * of the TXFIFO sizes when resizing internal memory for individual endpoints. 3067 * It will help ensured that the resizing logic reserves enough space for at 3068 * least one max packet. 3069 */ 3070 static int cdns3_gadget_check_config(struct usb_gadget *gadget) 3071 { 3072 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget); 3073 struct cdns3_endpoint *priv_ep; 3074 struct usb_ep *ep; 3075 int n_in = 0; 3076 int iso = 0; 3077 int out = 1; 3078 int total; 3079 int n; 3080 3081 list_for_each_entry(ep, &gadget->ep_list, ep_list) { 3082 priv_ep = ep_to_cdns3_ep(ep); 3083 if (!(priv_ep->flags & EP_CLAIMED)) 3084 continue; 3085 3086 n = (priv_ep->mult + 1) * (priv_ep->bMaxBurst + 1); 3087 if (ep->address & USB_DIR_IN) { 3088 /* 3089 * ISO transfer: DMA start move data when get ISO, only transfer 3090 * data as min(TD size, iso). No benefit for allocate bigger 3091 * internal memory than 'iso'. 3092 */ 3093 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) 3094 iso += n; 3095 else 3096 n_in++; 3097 } else { 3098 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) 3099 out = max_t(int, out, n); 3100 } 3101 } 3102 3103 /* 2KB are reserved for EP0, 1KB for out*/ 3104 total = 2 + n_in + out + iso; 3105 3106 if (total > priv_dev->onchip_buffers) 3107 return -ENOMEM; 3108 3109 priv_dev->ep_buf_size = (priv_dev->onchip_buffers - 2 - iso) / (n_in + out); 3110 3111 return 0; 3112 } 3113 3114 static const struct usb_gadget_ops cdns3_gadget_ops = { 3115 .get_frame = cdns3_gadget_get_frame, 3116 .wakeup = cdns3_gadget_wakeup, 3117 .set_selfpowered = cdns3_gadget_set_selfpowered, 3118 .pullup = cdns3_gadget_pullup, 3119 .udc_start = cdns3_gadget_udc_start, 3120 .udc_stop = cdns3_gadget_udc_stop, 3121 .match_ep = cdns3_gadget_match_ep, 3122 .check_config = cdns3_gadget_check_config, 3123 }; 3124 3125 static void cdns3_free_all_eps(struct cdns3_device *priv_dev) 3126 { 3127 int i; 3128 3129 /* ep0 OUT point to ep0 IN. */ 3130 priv_dev->eps[16] = NULL; 3131 3132 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) 3133 if (priv_dev->eps[i]) { 3134 cdns3_free_trb_pool(priv_dev->eps[i]); 3135 devm_kfree(priv_dev->dev, priv_dev->eps[i]); 3136 } 3137 } 3138 3139 /** 3140 * cdns3_init_eps - Initializes software endpoints of gadget 3141 * @priv_dev: extended gadget object 3142 * 3143 * Returns 0 on success, error code elsewhere 3144 */ 3145 static int cdns3_init_eps(struct cdns3_device *priv_dev) 3146 { 3147 u32 ep_enabled_reg, iso_ep_reg; 3148 struct cdns3_endpoint *priv_ep; 3149 int ep_dir, ep_number; 3150 u32 ep_mask; 3151 int ret = 0; 3152 int i; 3153 3154 /* Read it from USB_CAP3 to USB_CAP5 */ 3155 ep_enabled_reg = readl(&priv_dev->regs->usb_cap3); 3156 iso_ep_reg = readl(&priv_dev->regs->usb_cap4); 3157 3158 dev_dbg(priv_dev->dev, "Initializing non-zero endpoints\n"); 3159 3160 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) { 3161 ep_dir = i >> 4; /* i div 16 */ 3162 ep_number = i & 0xF; /* i % 16 */ 3163 ep_mask = BIT(i); 3164 3165 if (!(ep_enabled_reg & ep_mask)) 3166 continue; 3167 3168 if (ep_dir && !ep_number) { 3169 priv_dev->eps[i] = priv_dev->eps[0]; 3170 continue; 3171 } 3172 3173 priv_ep = devm_kzalloc(priv_dev->dev, sizeof(*priv_ep), 3174 GFP_KERNEL); 3175 if (!priv_ep) 3176 goto err; 3177 3178 /* set parent of endpoint object */ 3179 priv_ep->cdns3_dev = priv_dev; 3180 priv_dev->eps[i] = priv_ep; 3181 priv_ep->num = ep_number; 3182 priv_ep->dir = ep_dir ? USB_DIR_IN : USB_DIR_OUT; 3183 3184 if (!ep_number) { 3185 ret = cdns3_init_ep0(priv_dev, priv_ep); 3186 if (ret) { 3187 dev_err(priv_dev->dev, "Failed to init ep0\n"); 3188 goto err; 3189 } 3190 } else { 3191 snprintf(priv_ep->name, sizeof(priv_ep->name), "ep%d%s", 3192 ep_number, !!ep_dir ? "in" : "out"); 3193 priv_ep->endpoint.name = priv_ep->name; 3194 3195 usb_ep_set_maxpacket_limit(&priv_ep->endpoint, 3196 CDNS3_EP_MAX_PACKET_LIMIT); 3197 priv_ep->endpoint.max_streams = CDNS3_EP_MAX_STREAMS; 3198 priv_ep->endpoint.ops = &cdns3_gadget_ep_ops; 3199 if (ep_dir) 3200 priv_ep->endpoint.caps.dir_in = 1; 3201 else 3202 priv_ep->endpoint.caps.dir_out = 1; 3203 3204 if (iso_ep_reg & ep_mask) 3205 priv_ep->endpoint.caps.type_iso = 1; 3206 3207 priv_ep->endpoint.caps.type_bulk = 1; 3208 priv_ep->endpoint.caps.type_int = 1; 3209 3210 list_add_tail(&priv_ep->endpoint.ep_list, 3211 &priv_dev->gadget.ep_list); 3212 } 3213 3214 priv_ep->flags = 0; 3215 3216 dev_dbg(priv_dev->dev, "Initialized %s support: %s %s\n", 3217 priv_ep->name, 3218 priv_ep->endpoint.caps.type_bulk ? "BULK, INT" : "", 3219 priv_ep->endpoint.caps.type_iso ? "ISO" : ""); 3220 3221 INIT_LIST_HEAD(&priv_ep->pending_req_list); 3222 INIT_LIST_HEAD(&priv_ep->deferred_req_list); 3223 INIT_LIST_HEAD(&priv_ep->wa2_descmiss_req_list); 3224 } 3225 3226 return 0; 3227 err: 3228 cdns3_free_all_eps(priv_dev); 3229 return -ENOMEM; 3230 } 3231 3232 static void cdns3_gadget_release(struct device *dev) 3233 { 3234 struct cdns3_device *priv_dev = container_of(dev, 3235 struct cdns3_device, gadget.dev); 3236 3237 kfree(priv_dev); 3238 } 3239 3240 static void cdns3_gadget_exit(struct cdns *cdns) 3241 { 3242 struct cdns3_device *priv_dev; 3243 3244 priv_dev = cdns->gadget_dev; 3245 3246 3247 pm_runtime_mark_last_busy(cdns->dev); 3248 pm_runtime_put_autosuspend(cdns->dev); 3249 3250 usb_del_gadget(&priv_dev->gadget); 3251 devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev); 3252 3253 cdns3_free_all_eps(priv_dev); 3254 3255 while (!list_empty(&priv_dev->aligned_buf_list)) { 3256 struct cdns3_aligned_buf *buf; 3257 3258 buf = cdns3_next_align_buf(&priv_dev->aligned_buf_list); 3259 dma_free_noncoherent(priv_dev->sysdev, buf->size, 3260 buf->buf, 3261 buf->dma, 3262 buf->dir); 3263 3264 list_del(&buf->list); 3265 kfree(buf); 3266 } 3267 3268 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf, 3269 priv_dev->setup_dma); 3270 dma_pool_destroy(priv_dev->eps_dma_pool); 3271 3272 kfree(priv_dev->zlp_buf); 3273 usb_put_gadget(&priv_dev->gadget); 3274 cdns->gadget_dev = NULL; 3275 cdns_drd_gadget_off(cdns); 3276 } 3277 3278 static int cdns3_gadget_start(struct cdns *cdns) 3279 { 3280 struct cdns3_device *priv_dev; 3281 u32 max_speed; 3282 int ret; 3283 3284 priv_dev = kzalloc(sizeof(*priv_dev), GFP_KERNEL); 3285 if (!priv_dev) 3286 return -ENOMEM; 3287 3288 usb_initialize_gadget(cdns->dev, &priv_dev->gadget, 3289 cdns3_gadget_release); 3290 cdns->gadget_dev = priv_dev; 3291 priv_dev->sysdev = cdns->dev; 3292 priv_dev->dev = cdns->dev; 3293 priv_dev->regs = cdns->dev_regs; 3294 3295 device_property_read_u16(priv_dev->dev, "cdns,on-chip-buff-size", 3296 &priv_dev->onchip_buffers); 3297 3298 if (priv_dev->onchip_buffers <= 0) { 3299 u32 reg = readl(&priv_dev->regs->usb_cap2); 3300 3301 priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg); 3302 } 3303 3304 if (!priv_dev->onchip_buffers) 3305 priv_dev->onchip_buffers = 256; 3306 3307 max_speed = usb_get_maximum_speed(cdns->dev); 3308 3309 /* Check the maximum_speed parameter */ 3310 switch (max_speed) { 3311 case USB_SPEED_FULL: 3312 case USB_SPEED_HIGH: 3313 case USB_SPEED_SUPER: 3314 break; 3315 default: 3316 dev_err(cdns->dev, "invalid maximum_speed parameter %d\n", 3317 max_speed); 3318 fallthrough; 3319 case USB_SPEED_UNKNOWN: 3320 /* default to superspeed */ 3321 max_speed = USB_SPEED_SUPER; 3322 break; 3323 } 3324 3325 /* fill gadget fields */ 3326 priv_dev->gadget.max_speed = max_speed; 3327 priv_dev->gadget.speed = USB_SPEED_UNKNOWN; 3328 priv_dev->gadget.ops = &cdns3_gadget_ops; 3329 priv_dev->gadget.name = "usb-ss-gadget"; 3330 priv_dev->gadget.quirk_avoids_skb_reserve = 1; 3331 priv_dev->gadget.irq = cdns->dev_irq; 3332 3333 spin_lock_init(&priv_dev->lock); 3334 INIT_WORK(&priv_dev->pending_status_wq, 3335 cdns3_pending_setup_status_handler); 3336 3337 INIT_WORK(&priv_dev->aligned_buf_wq, 3338 cdns3_free_aligned_request_buf); 3339 3340 /* initialize endpoint container */ 3341 INIT_LIST_HEAD(&priv_dev->gadget.ep_list); 3342 INIT_LIST_HEAD(&priv_dev->aligned_buf_list); 3343 priv_dev->eps_dma_pool = dma_pool_create("cdns3_eps_dma_pool", 3344 priv_dev->sysdev, 3345 TRB_RING_SIZE, 8, 0); 3346 if (!priv_dev->eps_dma_pool) { 3347 dev_err(priv_dev->dev, "Failed to create TRB dma pool\n"); 3348 ret = -ENOMEM; 3349 goto err1; 3350 } 3351 3352 ret = cdns3_init_eps(priv_dev); 3353 if (ret) { 3354 dev_err(priv_dev->dev, "Failed to create endpoints\n"); 3355 goto err1; 3356 } 3357 3358 /* allocate memory for setup packet buffer */ 3359 priv_dev->setup_buf = dma_alloc_coherent(priv_dev->sysdev, 8, 3360 &priv_dev->setup_dma, GFP_DMA); 3361 if (!priv_dev->setup_buf) { 3362 ret = -ENOMEM; 3363 goto err2; 3364 } 3365 3366 priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6); 3367 3368 dev_dbg(priv_dev->dev, "Device Controller version: %08x\n", 3369 readl(&priv_dev->regs->usb_cap6)); 3370 dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n", 3371 readl(&priv_dev->regs->usb_cap1)); 3372 dev_dbg(priv_dev->dev, "On-Chip memory configuration: %08x\n", 3373 readl(&priv_dev->regs->usb_cap2)); 3374 3375 priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver); 3376 if (priv_dev->dev_ver >= DEV_VER_V2) 3377 priv_dev->gadget.sg_supported = 1; 3378 3379 priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL); 3380 if (!priv_dev->zlp_buf) { 3381 ret = -ENOMEM; 3382 goto err3; 3383 } 3384 3385 /* add USB gadget device */ 3386 ret = usb_add_gadget(&priv_dev->gadget); 3387 if (ret < 0) { 3388 dev_err(priv_dev->dev, "Failed to add gadget\n"); 3389 goto err4; 3390 } 3391 3392 return 0; 3393 err4: 3394 kfree(priv_dev->zlp_buf); 3395 err3: 3396 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf, 3397 priv_dev->setup_dma); 3398 err2: 3399 cdns3_free_all_eps(priv_dev); 3400 err1: 3401 dma_pool_destroy(priv_dev->eps_dma_pool); 3402 3403 usb_put_gadget(&priv_dev->gadget); 3404 cdns->gadget_dev = NULL; 3405 return ret; 3406 } 3407 3408 static int __cdns3_gadget_init(struct cdns *cdns) 3409 { 3410 int ret = 0; 3411 3412 /* Ensure 32-bit DMA Mask in case we switched back from Host mode */ 3413 ret = dma_set_mask_and_coherent(cdns->dev, DMA_BIT_MASK(32)); 3414 if (ret) { 3415 dev_err(cdns->dev, "Failed to set dma mask: %d\n", ret); 3416 return ret; 3417 } 3418 3419 cdns_drd_gadget_on(cdns); 3420 pm_runtime_get_sync(cdns->dev); 3421 3422 ret = cdns3_gadget_start(cdns); 3423 if (ret) { 3424 pm_runtime_put_sync(cdns->dev); 3425 return ret; 3426 } 3427 3428 /* 3429 * Because interrupt line can be shared with other components in 3430 * driver it can't use IRQF_ONESHOT flag here. 3431 */ 3432 ret = devm_request_threaded_irq(cdns->dev, cdns->dev_irq, 3433 cdns3_device_irq_handler, 3434 cdns3_device_thread_irq_handler, 3435 IRQF_SHARED, dev_name(cdns->dev), 3436 cdns->gadget_dev); 3437 3438 if (ret) 3439 goto err0; 3440 3441 return 0; 3442 err0: 3443 cdns3_gadget_exit(cdns); 3444 return ret; 3445 } 3446 3447 static int cdns3_gadget_suspend(struct cdns *cdns, bool do_wakeup) 3448 __must_hold(&cdns->lock) 3449 { 3450 struct cdns3_device *priv_dev = cdns->gadget_dev; 3451 3452 spin_unlock(&cdns->lock); 3453 cdns3_disconnect_gadget(priv_dev); 3454 spin_lock(&cdns->lock); 3455 3456 priv_dev->gadget.speed = USB_SPEED_UNKNOWN; 3457 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED); 3458 cdns3_hw_reset_eps_config(priv_dev); 3459 3460 /* disable interrupt for device */ 3461 writel(0, &priv_dev->regs->usb_ien); 3462 3463 return 0; 3464 } 3465 3466 static int cdns3_gadget_resume(struct cdns *cdns, bool hibernated) 3467 { 3468 struct cdns3_device *priv_dev = cdns->gadget_dev; 3469 3470 if (!priv_dev->gadget_driver) 3471 return 0; 3472 3473 cdns3_gadget_config(priv_dev); 3474 if (hibernated) 3475 writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf); 3476 3477 return 0; 3478 } 3479 3480 /** 3481 * cdns3_gadget_init - initialize device structure 3482 * 3483 * @cdns: cdns instance 3484 * 3485 * This function initializes the gadget. 3486 */ 3487 int cdns3_gadget_init(struct cdns *cdns) 3488 { 3489 struct cdns_role_driver *rdrv; 3490 3491 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL); 3492 if (!rdrv) 3493 return -ENOMEM; 3494 3495 rdrv->start = __cdns3_gadget_init; 3496 rdrv->stop = cdns3_gadget_exit; 3497 rdrv->suspend = cdns3_gadget_suspend; 3498 rdrv->resume = cdns3_gadget_resume; 3499 rdrv->state = CDNS_ROLE_STATE_INACTIVE; 3500 rdrv->name = "gadget"; 3501 cdns->roles[USB_ROLE_DEVICE] = rdrv; 3502 3503 return 0; 3504 } 3505