1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence USBSS DRD Driver - gadget side.
5 * Copyright (C) 2018-2019 Cadence Design Systems.
6 * Copyright (C) 2017-2018 NXP
8 * Authors: Pawel Jez <pjez@cadence.com>,
9 * Pawel Laszczak <pawell@cadence.com>
10 * Peter Chen <peter.chen@nxp.com>
15 * At some situations, the controller may get stale data address in TRB
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
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
29 * Issue has been fixed in DEV_VER_V3 version of controller.
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
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.
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.
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.
47 * Driver use limited number of buffer. This number can be set by macro
48 * CDNS3_WA2_NUM_BUFFERS.
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
55 * Issue has been fixed in DEV_VER_V2 version of controller.
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>
66 #include "gadget-export.h"
67 #include "cdns3-gadget.h"
68 #include "cdns3-trace.h"
71 static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
72 struct usb_request *request,
75 static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
76 struct usb_request *request);
78 static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
79 struct usb_request *request);
82 * cdns3_clear_register_bit - clear bit in given register.
83 * @ptr: address of device controller register to be read and changed
84 * @mask: bits requested to clar
86 static void cdns3_clear_register_bit(void __iomem *ptr, u32 mask)
88 mask = readl(ptr) & ~mask;
93 * cdns3_set_register_bit - set bit in given register.
94 * @ptr: address of device controller register to be read and changed
95 * @mask: bits requested to set
97 void cdns3_set_register_bit(void __iomem *ptr, u32 mask)
99 mask = readl(ptr) | mask;
104 * cdns3_ep_addr_to_index - Macro converts endpoint address to
105 * index of endpoint object in cdns3_device.eps[] container
106 * @ep_addr: endpoint address for which endpoint object is required
109 u8 cdns3_ep_addr_to_index(u8 ep_addr)
111 return (((ep_addr & 0x7F)) + ((ep_addr & USB_DIR_IN) ? 16 : 0));
114 static int cdns3_get_dma_pos(struct cdns3_device *priv_dev,
115 struct cdns3_endpoint *priv_ep)
119 dma_index = readl(&priv_dev->regs->ep_traddr) - priv_ep->trb_pool_dma;
121 return dma_index / TRB_SIZE;
125 * cdns3_next_request - returns next request from list
126 * @list: list containing requests
128 * Returns request or NULL if no requests in list
130 struct usb_request *cdns3_next_request(struct list_head *list)
132 return list_first_entry_or_null(list, struct usb_request, list);
136 * cdns3_next_align_buf - returns next buffer from list
137 * @list: list containing buffers
139 * Returns buffer or NULL if no buffers in list
141 static struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list)
143 return list_first_entry_or_null(list, struct cdns3_aligned_buf, list);
147 * cdns3_next_priv_request - returns next request from list
148 * @list: list containing requests
150 * Returns request or NULL if no requests in list
152 static struct cdns3_request *cdns3_next_priv_request(struct list_head *list)
154 return list_first_entry_or_null(list, struct cdns3_request, list);
158 * cdns3_select_ep - selects endpoint
159 * @priv_dev: extended gadget object
160 * @ep: endpoint address
162 void cdns3_select_ep(struct cdns3_device *priv_dev, u32 ep)
164 if (priv_dev->selected_ep == ep)
167 priv_dev->selected_ep = ep;
168 writel(ep, &priv_dev->regs->ep_sel);
172 * cdns3_get_tdl - gets current tdl for selected endpoint.
173 * @priv_dev: extended gadget object
175 * Before calling this function the appropriate endpoint must
176 * be selected by means of cdns3_select_ep function.
178 static int cdns3_get_tdl(struct cdns3_device *priv_dev)
180 if (priv_dev->dev_ver < DEV_VER_V3)
181 return EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
183 return readl(&priv_dev->regs->ep_tdl);
186 dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep,
187 struct cdns3_trb *trb)
189 u32 offset = (char *)trb - (char *)priv_ep->trb_pool;
191 return priv_ep->trb_pool_dma + offset;
194 static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
196 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
198 if (priv_ep->trb_pool) {
199 dma_pool_free(priv_dev->eps_dma_pool,
200 priv_ep->trb_pool, priv_ep->trb_pool_dma);
201 priv_ep->trb_pool = NULL;
206 * cdns3_allocate_trb_pool - Allocates TRB's pool for selected endpoint
207 * @priv_ep: endpoint object
209 * Function will return 0 on success or -ENOMEM on allocation error
211 int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
213 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
214 int ring_size = TRB_RING_SIZE;
215 int num_trbs = ring_size / TRB_SIZE;
216 struct cdns3_trb *link_trb;
218 if (priv_ep->trb_pool && priv_ep->alloc_ring_size < ring_size)
219 cdns3_free_trb_pool(priv_ep);
221 if (!priv_ep->trb_pool) {
222 priv_ep->trb_pool = dma_pool_alloc(priv_dev->eps_dma_pool,
223 GFP_DMA32 | GFP_ATOMIC,
224 &priv_ep->trb_pool_dma);
226 if (!priv_ep->trb_pool)
229 priv_ep->alloc_ring_size = ring_size;
232 memset(priv_ep->trb_pool, 0, ring_size);
234 priv_ep->num_trbs = num_trbs;
239 /* Initialize the last TRB as Link TRB */
240 link_trb = (priv_ep->trb_pool + (priv_ep->num_trbs - 1));
242 if (priv_ep->use_streams) {
244 * For stream capable endpoints driver use single correct TRB.
245 * The last trb has zeroed cycle bit
247 link_trb->control = 0;
249 link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma));
250 link_trb->control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_LINK) | TRB_TOGGLE);
256 * cdns3_ep_stall_flush - Stalls and flushes selected endpoint
257 * @priv_ep: endpoint object
259 * Endpoint must be selected before call to this function
261 static void cdns3_ep_stall_flush(struct cdns3_endpoint *priv_ep)
263 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
266 trace_cdns3_halt(priv_ep, 1, 1);
268 writel(EP_CMD_DFLUSH | EP_CMD_ERDY | EP_CMD_SSTALL,
269 &priv_dev->regs->ep_cmd);
271 /* wait for DFLUSH cleared */
272 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
273 !(val & EP_CMD_DFLUSH), 1, 1000);
274 priv_ep->flags |= EP_STALLED;
275 priv_ep->flags &= ~EP_STALL_PENDING;
279 * cdns3_hw_reset_eps_config - reset endpoints configuration kept by controller.
280 * @priv_dev: extended gadget object
282 void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev)
286 writel(USB_CONF_CFGRST, &priv_dev->regs->usb_conf);
288 cdns3_allow_enable_l1(priv_dev, 0);
289 priv_dev->hw_configured_flag = 0;
290 priv_dev->onchip_used_size = 0;
291 priv_dev->out_mem_is_allocated = 0;
292 priv_dev->wait_for_setup = 0;
293 priv_dev->using_streams = 0;
295 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
296 if (priv_dev->eps[i])
297 priv_dev->eps[i]->flags &= ~EP_CONFIGURED;
301 * cdns3_ep_inc_trb - increment a trb index.
302 * @index: Pointer to the TRB index to increment.
304 * @trb_in_seg: number of TRBs in segment
306 * The index should never point to the link TRB. After incrementing,
307 * if it is point to the link TRB, wrap around to the beginning and revert
308 * cycle state bit The
309 * link TRB is always at the last TRB entry.
311 static void cdns3_ep_inc_trb(int *index, u8 *cs, int trb_in_seg)
314 if (*index == (trb_in_seg - 1)) {
321 * cdns3_ep_inc_enq - increment endpoint's enqueue pointer
322 * @priv_ep: The endpoint whose enqueue pointer we're incrementing
324 static void cdns3_ep_inc_enq(struct cdns3_endpoint *priv_ep)
326 priv_ep->free_trbs--;
327 cdns3_ep_inc_trb(&priv_ep->enqueue, &priv_ep->pcs, priv_ep->num_trbs);
331 * cdns3_ep_inc_deq - increment endpoint's dequeue pointer
332 * @priv_ep: The endpoint whose dequeue pointer we're incrementing
334 static void cdns3_ep_inc_deq(struct cdns3_endpoint *priv_ep)
336 priv_ep->free_trbs++;
337 cdns3_ep_inc_trb(&priv_ep->dequeue, &priv_ep->ccs, priv_ep->num_trbs);
340 static void cdns3_move_deq_to_next_trb(struct cdns3_request *priv_req)
342 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
343 int current_trb = priv_req->start_trb;
345 while (current_trb != priv_req->end_trb) {
346 cdns3_ep_inc_deq(priv_ep);
347 current_trb = priv_ep->dequeue;
350 cdns3_ep_inc_deq(priv_ep);
354 * cdns3_allow_enable_l1 - enable/disable permits to transition to L1.
355 * @priv_dev: Extended gadget object
356 * @enable: Enable/disable permit to transition to L1.
358 * If bit USB_CONF_L1EN is set and device receive Extended Token packet,
359 * then controller answer with ACK handshake.
360 * If bit USB_CONF_L1DS is set and device receive Extended Token packet,
361 * then controller answer with NYET handshake.
363 void cdns3_allow_enable_l1(struct cdns3_device *priv_dev, int enable)
366 writel(USB_CONF_L1EN, &priv_dev->regs->usb_conf);
368 writel(USB_CONF_L1DS, &priv_dev->regs->usb_conf);
371 enum usb_device_speed cdns3_get_speed(struct cdns3_device *priv_dev)
375 reg = readl(&priv_dev->regs->usb_sts);
377 if (DEV_SUPERSPEED(reg))
378 return USB_SPEED_SUPER;
379 else if (DEV_HIGHSPEED(reg))
380 return USB_SPEED_HIGH;
381 else if (DEV_FULLSPEED(reg))
382 return USB_SPEED_FULL;
383 else if (DEV_LOWSPEED(reg))
384 return USB_SPEED_LOW;
385 return USB_SPEED_UNKNOWN;
389 * cdns3_start_all_request - add to ring all request not started
390 * @priv_dev: Extended gadget object
391 * @priv_ep: The endpoint for whom request will be started.
393 * Returns return ENOMEM if transfer ring i not enough TRBs to start
396 static int cdns3_start_all_request(struct cdns3_device *priv_dev,
397 struct cdns3_endpoint *priv_ep)
399 struct usb_request *request;
401 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
404 * If the last pending transfer is INTERNAL
405 * OR streams are enabled for this endpoint
406 * do NOT start new transfer till the last one is pending
408 if (!pending_empty) {
409 struct cdns3_request *priv_req;
411 request = cdns3_next_request(&priv_ep->pending_req_list);
412 priv_req = to_cdns3_request(request);
413 if ((priv_req->flags & REQUEST_INTERNAL) ||
414 (priv_ep->flags & EP_TDLCHK_EN) ||
415 priv_ep->use_streams) {
416 dev_dbg(priv_dev->dev, "Blocking external request\n");
421 while (!list_empty(&priv_ep->deferred_req_list)) {
422 request = cdns3_next_request(&priv_ep->deferred_req_list);
424 if (!priv_ep->use_streams) {
425 ret = cdns3_ep_run_transfer(priv_ep, request);
427 priv_ep->stream_sg_idx = 0;
428 ret = cdns3_ep_run_stream_transfer(priv_ep, request);
433 list_move_tail(&request->list, &priv_ep->pending_req_list);
434 if (request->stream_id != 0 || (priv_ep->flags & EP_TDLCHK_EN))
438 priv_ep->flags &= ~EP_RING_FULL;
443 * WA2: Set flag for all not ISOC OUT endpoints. If this flag is set
444 * driver try to detect whether endpoint need additional internal
445 * buffer for unblocking on-chip FIFO buffer. This flag will be cleared
446 * if before first DESCMISS interrupt the DMA will be armed.
448 #define cdns3_wa2_enable_detection(priv_dev, priv_ep, reg) do { \
449 if (!priv_ep->dir && priv_ep->type != USB_ENDPOINT_XFER_ISOC) { \
450 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_DET; \
451 (reg) |= EP_STS_EN_DESCMISEN; \
454 static void __cdns3_descmiss_copy_data(struct usb_request *request,
455 struct usb_request *descmiss_req)
457 int length = request->actual + descmiss_req->actual;
458 struct scatterlist *s = request->sg;
461 if (length <= request->length) {
462 memcpy(&((u8 *)request->buf)[request->actual],
464 descmiss_req->actual);
465 request->actual = length;
467 /* It should never occures */
468 request->status = -ENOMEM;
471 if (length <= sg_dma_len(s)) {
472 void *p = phys_to_virt(sg_dma_address(s));
474 memcpy(&((u8 *)p)[request->actual],
476 descmiss_req->actual);
477 request->actual = length;
479 request->status = -ENOMEM;
485 * cdns3_wa2_descmiss_copy_data - copy data from internal requests to
486 * request queued by class driver.
487 * @priv_ep: extended endpoint object
488 * @request: request object
490 static void cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint *priv_ep,
491 struct usb_request *request)
493 struct usb_request *descmiss_req;
494 struct cdns3_request *descmiss_priv_req;
496 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
500 cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
501 descmiss_req = &descmiss_priv_req->request;
503 /* driver can't touch pending request */
504 if (descmiss_priv_req->flags & REQUEST_PENDING)
507 chunk_end = descmiss_priv_req->flags & REQUEST_INTERNAL_CH;
508 request->status = descmiss_req->status;
509 __cdns3_descmiss_copy_data(request, descmiss_req);
510 list_del_init(&descmiss_priv_req->list);
511 kfree(descmiss_req->buf);
512 cdns3_gadget_ep_free_request(&priv_ep->endpoint, descmiss_req);
513 --priv_ep->wa2_counter;
520 static struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev,
521 struct cdns3_endpoint *priv_ep,
522 struct cdns3_request *priv_req)
524 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN &&
525 priv_req->flags & REQUEST_INTERNAL) {
526 struct usb_request *req;
528 req = cdns3_next_request(&priv_ep->deferred_req_list);
530 priv_ep->descmis_req = NULL;
535 /* unmap the gadget request before copying data */
536 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, req,
539 cdns3_wa2_descmiss_copy_data(priv_ep, req);
540 if (!(priv_ep->flags & EP_QUIRK_END_TRANSFER) &&
541 req->length != req->actual) {
542 /* wait for next part of transfer */
543 /* re-map the gadget request buffer*/
544 usb_gadget_map_request_by_dev(priv_dev->sysdev, req,
545 usb_endpoint_dir_in(priv_ep->endpoint.desc));
549 if (req->status == -EINPROGRESS)
552 list_del_init(&req->list);
553 cdns3_start_all_request(priv_dev, priv_ep);
557 return &priv_req->request;
560 static int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev,
561 struct cdns3_endpoint *priv_ep,
562 struct cdns3_request *priv_req)
567 * If transfer was queued before DESCMISS appear than we
568 * can disable handling of DESCMISS interrupt. Driver assumes that it
569 * can disable special treatment for this endpoint.
571 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
574 cdns3_select_ep(priv_dev, priv_ep->num | priv_ep->dir);
575 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
576 reg = readl(&priv_dev->regs->ep_sts_en);
577 reg &= ~EP_STS_EN_DESCMISEN;
578 trace_cdns3_wa2(priv_ep, "workaround disabled\n");
579 writel(reg, &priv_dev->regs->ep_sts_en);
582 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
583 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
584 u8 descmiss_empty = list_empty(&priv_ep->wa2_descmiss_req_list);
587 * DESCMISS transfer has been finished, so data will be
588 * directly copied from internal allocated usb_request
591 if (pending_empty && !descmiss_empty &&
592 !(priv_req->flags & REQUEST_INTERNAL)) {
593 cdns3_wa2_descmiss_copy_data(priv_ep,
596 trace_cdns3_wa2(priv_ep, "get internal stored data");
598 list_add_tail(&priv_req->request.list,
599 &priv_ep->pending_req_list);
600 cdns3_gadget_giveback(priv_ep, priv_req,
601 priv_req->request.status);
604 * Intentionally driver returns positive value as
605 * correct value. It informs that transfer has
612 * Driver will wait for completion DESCMISS transfer,
613 * before starts new, not DESCMISS transfer.
615 if (!pending_empty && !descmiss_empty) {
616 trace_cdns3_wa2(priv_ep, "wait for pending transfer\n");
620 if (priv_req->flags & REQUEST_INTERNAL)
621 list_add_tail(&priv_req->list,
622 &priv_ep->wa2_descmiss_req_list);
628 static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep)
630 struct cdns3_request *priv_req;
632 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
635 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
636 chain = !!(priv_req->flags & REQUEST_INTERNAL_CH);
638 trace_cdns3_wa2(priv_ep, "removes eldest request");
640 kfree(priv_req->request.buf);
641 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
643 list_del_init(&priv_req->list);
644 --priv_ep->wa2_counter;
652 * cdns3_wa2_descmissing_packet - handles descriptor missing event.
653 * @priv_ep: extended gadget object
655 * This function is used only for WA2. For more information see Work around 2
658 static void cdns3_wa2_descmissing_packet(struct cdns3_endpoint *priv_ep)
660 struct cdns3_request *priv_req;
661 struct usb_request *request;
662 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
664 /* check for pending transfer */
665 if (!pending_empty) {
666 trace_cdns3_wa2(priv_ep, "Ignoring Descriptor missing IRQ\n");
670 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
671 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
672 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_EN;
675 trace_cdns3_wa2(priv_ep, "Description Missing detected\n");
677 if (priv_ep->wa2_counter >= CDNS3_WA2_NUM_BUFFERS) {
678 trace_cdns3_wa2(priv_ep, "WA2 overflow\n");
679 cdns3_wa2_remove_old_request(priv_ep);
682 request = cdns3_gadget_ep_alloc_request(&priv_ep->endpoint,
687 priv_req = to_cdns3_request(request);
688 priv_req->flags |= REQUEST_INTERNAL;
690 /* if this field is still assigned it indicate that transfer related
691 * with this request has not been finished yet. Driver in this
692 * case simply allocate next request and assign flag REQUEST_INTERNAL_CH
693 * flag to previous one. It will indicate that current request is
694 * part of the previous one.
696 if (priv_ep->descmis_req)
697 priv_ep->descmis_req->flags |= REQUEST_INTERNAL_CH;
699 priv_req->request.buf = kzalloc(CDNS3_DESCMIS_BUF_SIZE,
701 priv_ep->wa2_counter++;
703 if (!priv_req->request.buf) {
704 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
708 priv_req->request.length = CDNS3_DESCMIS_BUF_SIZE;
709 priv_ep->descmis_req = priv_req;
711 __cdns3_gadget_ep_queue(&priv_ep->endpoint,
712 &priv_ep->descmis_req->request,
718 dev_err(priv_ep->cdns3_dev->dev,
719 "Failed: No sufficient memory for DESCMIS\n");
722 static void cdns3_wa2_reset_tdl(struct cdns3_device *priv_dev)
724 u16 tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
727 u16 reset_val = EP_CMD_TDL_MAX + 1 - tdl;
729 writel(EP_CMD_TDL_SET(reset_val) | EP_CMD_STDL,
730 &priv_dev->regs->ep_cmd);
734 static void cdns3_wa2_check_outq_status(struct cdns3_device *priv_dev)
739 cdns3_select_ep(priv_dev, 0);
741 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
743 if (EP_STS_OUTQ_VAL(ep_sts_reg)) {
744 u32 outq_ep_num = EP_STS_OUTQ_NO(ep_sts_reg);
745 struct cdns3_endpoint *outq_ep = priv_dev->eps[outq_ep_num];
747 if ((outq_ep->flags & EP_ENABLED) && !(outq_ep->use_streams) &&
748 outq_ep->type != USB_ENDPOINT_XFER_ISOC && outq_ep_num) {
749 u8 pending_empty = list_empty(&outq_ep->pending_req_list);
751 if ((outq_ep->flags & EP_QUIRK_EXTRA_BUF_DET) ||
752 (outq_ep->flags & EP_QUIRK_EXTRA_BUF_EN) ||
758 cdns3_select_ep(priv_dev, outq_ep->num |
760 ep_sts_en_reg = readl(&priv_dev->regs->ep_sts_en);
761 ep_cmd_reg = readl(&priv_dev->regs->ep_cmd);
763 outq_ep->flags |= EP_TDLCHK_EN;
764 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
767 cdns3_wa2_enable_detection(priv_dev, outq_ep,
769 writel(ep_sts_en_reg,
770 &priv_dev->regs->ep_sts_en);
771 /* reset tdl value to zero */
772 cdns3_wa2_reset_tdl(priv_dev);
774 * Memory barrier - Reset tdl before ringing the
778 if (EP_CMD_DRDY & ep_cmd_reg) {
779 trace_cdns3_wa2(outq_ep, "Enabling WA2 skipping doorbell\n");
782 trace_cdns3_wa2(outq_ep, "Enabling WA2 ringing doorbell\n");
784 * ring doorbell to generate DESCMIS irq
787 &priv_dev->regs->ep_cmd);
795 * cdns3_gadget_giveback - call struct usb_request's ->complete callback
796 * @priv_ep: The endpoint to whom the request belongs to
797 * @priv_req: The request we're giving back
798 * @status: completion code for the request
800 * Must be called with controller's lock held and interrupts disabled. This
801 * function will unmap @req and call its ->complete() callback to notify upper
802 * layers that it has completed.
804 void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
805 struct cdns3_request *priv_req,
808 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
809 struct usb_request *request = &priv_req->request;
811 list_del_init(&request->list);
813 if (request->status == -EINPROGRESS)
814 request->status = status;
816 usb_gadget_unmap_request_by_dev(priv_dev->sysdev, request,
819 if ((priv_req->flags & REQUEST_UNALIGNED) &&
820 priv_ep->dir == USB_DIR_OUT && !request->status) {
821 /* Make DMA buffer CPU accessible */
822 dma_sync_single_for_cpu(priv_dev->sysdev,
823 priv_req->aligned_buf->dma,
824 priv_req->aligned_buf->size,
825 priv_req->aligned_buf->dir);
826 memcpy(request->buf, priv_req->aligned_buf->buf,
830 priv_req->flags &= ~(REQUEST_PENDING | REQUEST_UNALIGNED);
831 /* All TRBs have finished, clear the counter */
832 priv_req->finished_trb = 0;
833 trace_cdns3_gadget_giveback(priv_req);
835 if (priv_dev->dev_ver < DEV_VER_V2) {
836 request = cdns3_wa2_gadget_giveback(priv_dev, priv_ep,
842 if (request->complete) {
843 spin_unlock(&priv_dev->lock);
844 usb_gadget_giveback_request(&priv_ep->endpoint,
846 spin_lock(&priv_dev->lock);
849 if (request->buf == priv_dev->zlp_buf)
850 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
853 static void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep)
855 /* Work around for stale data address in TRB*/
856 if (priv_ep->wa1_set) {
857 trace_cdns3_wa1(priv_ep, "restore cycle bit");
859 priv_ep->wa1_set = 0;
860 priv_ep->wa1_trb_index = 0xFFFF;
861 if (priv_ep->wa1_cycle_bit) {
862 priv_ep->wa1_trb->control =
863 priv_ep->wa1_trb->control | cpu_to_le32(0x1);
865 priv_ep->wa1_trb->control =
866 priv_ep->wa1_trb->control & cpu_to_le32(~0x1);
871 static void cdns3_free_aligned_request_buf(struct work_struct *work)
873 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
875 struct cdns3_aligned_buf *buf, *tmp;
878 spin_lock_irqsave(&priv_dev->lock, flags);
880 list_for_each_entry_safe(buf, tmp, &priv_dev->aligned_buf_list, list) {
882 list_del(&buf->list);
885 * Re-enable interrupts to free DMA capable memory.
886 * Driver can't free this memory with disabled
889 spin_unlock_irqrestore(&priv_dev->lock, flags);
890 dma_free_noncoherent(priv_dev->sysdev, buf->size,
891 buf->buf, buf->dma, buf->dir);
893 spin_lock_irqsave(&priv_dev->lock, flags);
897 spin_unlock_irqrestore(&priv_dev->lock, flags);
900 static int cdns3_prepare_aligned_request_buf(struct cdns3_request *priv_req)
902 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
903 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
904 struct cdns3_aligned_buf *buf;
906 /* check if buffer is aligned to 8. */
907 if (!((uintptr_t)priv_req->request.buf & 0x7))
910 buf = priv_req->aligned_buf;
912 if (!buf || priv_req->request.length > buf->size) {
913 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
917 buf->size = priv_req->request.length;
918 buf->dir = usb_endpoint_dir_in(priv_ep->endpoint.desc) ?
919 DMA_TO_DEVICE : DMA_FROM_DEVICE;
921 buf->buf = dma_alloc_noncoherent(priv_dev->sysdev,
931 if (priv_req->aligned_buf) {
932 trace_cdns3_free_aligned_request(priv_req);
933 priv_req->aligned_buf->in_use = 0;
934 queue_work(system_freezable_wq,
935 &priv_dev->aligned_buf_wq);
939 priv_req->aligned_buf = buf;
941 list_add_tail(&buf->list,
942 &priv_dev->aligned_buf_list);
945 if (priv_ep->dir == USB_DIR_IN) {
946 /* Make DMA buffer CPU accessible */
947 dma_sync_single_for_cpu(priv_dev->sysdev,
948 buf->dma, buf->size, buf->dir);
949 memcpy(buf->buf, priv_req->request.buf,
950 priv_req->request.length);
953 /* Transfer DMA buffer ownership back to device */
954 dma_sync_single_for_device(priv_dev->sysdev,
955 buf->dma, buf->size, buf->dir);
957 priv_req->flags |= REQUEST_UNALIGNED;
958 trace_cdns3_prepare_aligned_request(priv_req);
963 static int cdns3_wa1_update_guard(struct cdns3_endpoint *priv_ep,
964 struct cdns3_trb *trb)
966 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
968 if (!priv_ep->wa1_set) {
971 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
974 priv_ep->wa1_cycle_bit = priv_ep->pcs ? TRB_CYCLE : 0;
975 priv_ep->wa1_set = 1;
976 priv_ep->wa1_trb = trb;
977 priv_ep->wa1_trb_index = priv_ep->enqueue;
978 trace_cdns3_wa1(priv_ep, "set guard");
985 static void cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device *priv_dev,
986 struct cdns3_endpoint *priv_ep)
991 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
992 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
994 if (!doorbell || dma_index != priv_ep->wa1_trb_index)
995 cdns3_wa1_restore_cycle_bit(priv_ep);
998 static int cdns3_ep_run_stream_transfer(struct cdns3_endpoint *priv_ep,
999 struct usb_request *request)
1001 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1002 struct cdns3_request *priv_req;
1003 struct cdns3_trb *trb;
1009 unsigned int sg_idx = priv_ep->stream_sg_idx;
1011 priv_req = to_cdns3_request(request);
1012 address = priv_ep->endpoint.desc->bEndpointAddress;
1014 priv_ep->flags |= EP_PENDING_REQUEST;
1016 /* must allocate buffer aligned to 8 */
1017 if (priv_req->flags & REQUEST_UNALIGNED)
1018 trb_dma = priv_req->aligned_buf->dma;
1020 trb_dma = request->dma;
1022 /* For stream capable endpoints driver use only single TD. */
1023 trb = priv_ep->trb_pool + priv_ep->enqueue;
1024 priv_req->start_trb = priv_ep->enqueue;
1025 priv_req->end_trb = priv_req->start_trb;
1026 priv_req->trb = trb;
1028 cdns3_select_ep(priv_ep->cdns3_dev, address);
1030 control = TRB_TYPE(TRB_NORMAL) | TRB_CYCLE |
1031 TRB_STREAM_ID(priv_req->request.stream_id) | TRB_ISP;
1033 if (!request->num_sgs) {
1034 trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
1035 length = request->length;
1037 trb->buffer = cpu_to_le32(TRB_BUFFER(request->sg[sg_idx].dma_address));
1038 length = request->sg[sg_idx].length;
1041 tdl = DIV_ROUND_UP(length, priv_ep->endpoint.maxpacket);
1043 trb->length = cpu_to_le32(TRB_BURST_LEN(16) | TRB_LEN(length));
1046 * For DEV_VER_V2 controller version we have enabled
1047 * USB_CONF2_EN_TDL_TRB in DMULT configuration.
1048 * This enables TDL calculation based on TRB, hence setting TDL in TRB.
1050 if (priv_dev->dev_ver >= DEV_VER_V2) {
1051 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
1052 trb->length |= cpu_to_le32(TRB_TDL_SS_SIZE(tdl));
1054 priv_req->flags |= REQUEST_PENDING;
1056 trb->control = cpu_to_le32(control);
1058 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1061 * Memory barrier - Cycle Bit must be set before trb->length and
1062 * trb->buffer fields.
1066 /* always first element */
1067 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma),
1068 &priv_dev->regs->ep_traddr);
1070 if (!(priv_ep->flags & EP_STALLED)) {
1071 trace_cdns3_ring(priv_ep);
1072 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1073 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1075 priv_ep->prime_flag = false;
1078 * Controller version DEV_VER_V2 tdl calculation
1082 if (priv_dev->dev_ver < DEV_VER_V2)
1083 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1084 &priv_dev->regs->ep_cmd);
1085 else if (priv_dev->dev_ver > DEV_VER_V2)
1086 writel(tdl, &priv_dev->regs->ep_tdl);
1088 priv_ep->last_stream_id = priv_req->request.stream_id;
1089 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1090 writel(EP_CMD_ERDY_SID(priv_req->request.stream_id) |
1091 EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
1093 trace_cdns3_doorbell_epx(priv_ep->name,
1094 readl(&priv_dev->regs->ep_traddr));
1097 /* WORKAROUND for transition to L0 */
1098 __cdns3_gadget_wakeup(priv_dev);
1103 static void cdns3_rearm_drdy_if_needed(struct cdns3_endpoint *priv_ep)
1105 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1107 if (priv_dev->dev_ver < DEV_VER_V3)
1110 if (readl(&priv_dev->regs->ep_sts) & EP_STS_TRBERR) {
1111 writel(EP_STS_TRBERR, &priv_dev->regs->ep_sts);
1112 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1117 * cdns3_ep_run_transfer - start transfer on no-default endpoint hardware
1118 * @priv_ep: endpoint object
1119 * @request: request object
1121 * Returns zero on success or negative value on failure
1123 static int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
1124 struct usb_request *request)
1126 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1127 struct cdns3_request *priv_req;
1128 struct cdns3_trb *trb;
1129 struct cdns3_trb *link_trb = NULL;
1138 struct scatterlist *s = NULL;
1139 bool sg_supported = !!(request->num_mapped_sgs);
1141 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC)
1142 num_trb = priv_ep->interval;
1144 num_trb = sg_supported ? request->num_mapped_sgs : 1;
1146 if (num_trb > priv_ep->free_trbs) {
1147 priv_ep->flags |= EP_RING_FULL;
1151 priv_req = to_cdns3_request(request);
1152 address = priv_ep->endpoint.desc->bEndpointAddress;
1154 priv_ep->flags |= EP_PENDING_REQUEST;
1156 /* must allocate buffer aligned to 8 */
1157 if (priv_req->flags & REQUEST_UNALIGNED)
1158 trb_dma = priv_req->aligned_buf->dma;
1160 trb_dma = request->dma;
1162 trb = priv_ep->trb_pool + priv_ep->enqueue;
1163 priv_req->start_trb = priv_ep->enqueue;
1164 priv_req->trb = trb;
1166 cdns3_select_ep(priv_ep->cdns3_dev, address);
1169 if ((priv_ep->enqueue + num_trb) >= (priv_ep->num_trbs - 1)) {
1170 int doorbell, dma_index;
1173 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1174 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1176 /* Driver can't update LINK TRB if it is current processed. */
1177 if (doorbell && dma_index == priv_ep->num_trbs - 1) {
1178 priv_ep->flags |= EP_DEFERRED_DRDY;
1182 /*updating C bt in Link TRB before starting DMA*/
1183 link_trb = priv_ep->trb_pool + (priv_ep->num_trbs - 1);
1185 * For TRs size equal 2 enabling TRB_CHAIN for epXin causes
1186 * that DMA stuck at the LINK TRB.
1187 * On the other hand, removing TRB_CHAIN for longer TRs for
1188 * epXout cause that DMA stuck after handling LINK TRB.
1189 * To eliminate this strange behavioral driver set TRB_CHAIN
1190 * bit only for TR size > 2.
1192 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC ||
1193 TRBS_PER_SEGMENT > 2)
1196 link_trb->control = cpu_to_le32(((priv_ep->pcs) ? TRB_CYCLE : 0) |
1197 TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit);
1200 if (priv_dev->dev_ver <= DEV_VER_V2)
1201 togle_pcs = cdns3_wa1_update_guard(priv_ep, trb);
1206 /* set incorrect Cycle Bit for first trb*/
1207 control = priv_ep->pcs ? 0 : TRB_CYCLE;
1209 if (priv_dev->dev_ver >= DEV_VER_V2) {
1212 td_size = DIV_ROUND_UP(request->length,
1213 priv_ep->endpoint.maxpacket);
1214 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
1215 trb->length = cpu_to_le32(TRB_TDL_SS_SIZE(td_size));
1217 control |= TRB_TDL_HS_SIZE(td_size);
1224 control |= TRB_TYPE(TRB_NORMAL);
1226 trb->buffer = cpu_to_le32(TRB_BUFFER(sg_dma_address(s)));
1227 length = sg_dma_len(s);
1229 trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma));
1230 length = request->length;
1233 if (priv_ep->flags & EP_TDLCHK_EN)
1234 total_tdl += DIV_ROUND_UP(length,
1235 priv_ep->endpoint.maxpacket);
1237 trb->length |= cpu_to_le32(TRB_BURST_LEN(priv_ep->trb_burst_size) |
1239 pcs = priv_ep->pcs ? TRB_CYCLE : 0;
1242 * first trb should be prepared as last to avoid processing
1248 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
1249 control |= TRB_IOC | TRB_ISP;
1251 /* for last element in TD or in SG list */
1252 if (sg_iter == (num_trb - 1) && sg_iter != 0)
1253 control |= pcs | TRB_IOC | TRB_ISP;
1257 trb->control = cpu_to_le32(control);
1259 priv_req->trb->control = cpu_to_le32(control);
1262 trb->control |= cpu_to_le32(TRB_ISP);
1263 /* Don't set chain bit for last TRB */
1264 if (sg_iter < num_trb - 1)
1265 trb->control |= cpu_to_le32(TRB_CHAIN);
1272 priv_req->end_trb = priv_ep->enqueue;
1273 cdns3_ep_inc_enq(priv_ep);
1274 trb = priv_ep->trb_pool + priv_ep->enqueue;
1276 } while (sg_iter < num_trb);
1278 trb = priv_req->trb;
1280 priv_req->flags |= REQUEST_PENDING;
1281 priv_req->num_of_trb = num_trb;
1284 trb->control |= cpu_to_le32(TRB_IOC | TRB_ISP);
1286 if (priv_dev->dev_ver < DEV_VER_V2 &&
1287 (priv_ep->flags & EP_TDLCHK_EN)) {
1288 u16 tdl = total_tdl;
1289 u16 old_tdl = EP_CMD_TDL_GET(readl(&priv_dev->regs->ep_cmd));
1291 if (tdl > EP_CMD_TDL_MAX) {
1292 tdl = EP_CMD_TDL_MAX;
1293 priv_ep->pending_tdl = total_tdl - EP_CMD_TDL_MAX;
1296 if (old_tdl < tdl) {
1298 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL,
1299 &priv_dev->regs->ep_cmd);
1304 * Memory barrier - cycle bit must be set before other filds in trb.
1308 /* give the TD to the consumer*/
1310 trb->control = trb->control ^ cpu_to_le32(1);
1312 if (priv_dev->dev_ver <= DEV_VER_V2)
1313 cdns3_wa1_tray_restore_cycle_bit(priv_dev, priv_ep);
1318 while (i < num_trb) {
1319 trace_cdns3_prepare_trb(priv_ep, trb + i);
1320 if (trb + i == link_trb) {
1321 trb = priv_ep->trb_pool;
1322 num_trb = num_trb - i;
1329 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
1333 * Memory barrier - Cycle Bit must be set before trb->length and
1334 * trb->buffer fields.
1339 * For DMULT mode we can set address to transfer ring only once after
1340 * enabling endpoint.
1342 if (priv_ep->flags & EP_UPDATE_EP_TRBADDR) {
1344 * Until SW is not ready to handle the OUT transfer the ISO OUT
1345 * Endpoint should be disabled (EP_CFG.ENABLE = 0).
1346 * EP_CFG_ENABLE must be set before updating ep_traddr.
1348 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir &&
1349 !(priv_ep->flags & EP_QUIRK_ISO_OUT_EN)) {
1350 priv_ep->flags |= EP_QUIRK_ISO_OUT_EN;
1351 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
1355 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma +
1356 priv_req->start_trb * TRB_SIZE),
1357 &priv_dev->regs->ep_traddr);
1359 priv_ep->flags &= ~EP_UPDATE_EP_TRBADDR;
1362 if (!priv_ep->wa1_set && !(priv_ep->flags & EP_STALLED)) {
1363 trace_cdns3_ring(priv_ep);
1364 /*clearing TRBERR and EP_STS_DESCMIS before seting DRDY*/
1365 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1366 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1367 cdns3_rearm_drdy_if_needed(priv_ep);
1368 trace_cdns3_doorbell_epx(priv_ep->name,
1369 readl(&priv_dev->regs->ep_traddr));
1372 /* WORKAROUND for transition to L0 */
1373 __cdns3_gadget_wakeup(priv_dev);
1378 void cdns3_set_hw_configuration(struct cdns3_device *priv_dev)
1380 struct cdns3_endpoint *priv_ep;
1383 if (priv_dev->hw_configured_flag)
1386 writel(USB_CONF_CFGSET, &priv_dev->regs->usb_conf);
1388 cdns3_set_register_bit(&priv_dev->regs->usb_conf,
1389 USB_CONF_U1EN | USB_CONF_U2EN);
1391 priv_dev->hw_configured_flag = 1;
1393 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
1395 priv_ep = ep_to_cdns3_ep(ep);
1396 cdns3_start_all_request(priv_dev, priv_ep);
1400 cdns3_allow_enable_l1(priv_dev, 1);
1404 * cdns3_trb_handled - check whether trb has been handled by DMA
1406 * @priv_ep: extended endpoint object.
1407 * @priv_req: request object for checking
1409 * Endpoint must be selected before invoking this function.
1411 * Returns false if request has not been handled by DMA, else returns true.
1415 * DQ = priv_ep->dequeue - dequeue position
1416 * EQ = priv_ep->enqueue - enqueue position
1417 * ST = priv_req->start_trb - index of first TRB in transfer ring
1418 * ET = priv_req->end_trb - index of last TRB in transfer ring
1419 * CI = current_index - index of processed TRB by DMA.
1421 * As first step, we check if the TRB between the ST and ET.
1422 * Then, we check if cycle bit for index priv_ep->dequeue
1426 * 1. priv_ep->dequeue never equals to current_index.
1427 * 2 priv_ep->enqueue never exceed priv_ep->dequeue
1428 * 3. exception: priv_ep->enqueue == priv_ep->dequeue
1429 * and priv_ep->free_trbs is zero.
1430 * This case indicate that TR is full.
1432 * At below two cases, the request have been handled.
1433 * Case 1 - priv_ep->dequeue < current_index
1434 * SR ... EQ ... DQ ... CI ... ER
1435 * SR ... DQ ... CI ... EQ ... ER
1437 * Case 2 - priv_ep->dequeue > current_index
1438 * This situation takes place when CI go through the LINK TRB at the end of
1440 * SR ... CI ... EQ ... DQ ... ER
1442 static bool cdns3_trb_handled(struct cdns3_endpoint *priv_ep,
1443 struct cdns3_request *priv_req)
1445 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1446 struct cdns3_trb *trb;
1447 int current_index = 0;
1451 current_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1452 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1454 /* current trb doesn't belong to this request */
1455 if (priv_req->start_trb < priv_req->end_trb) {
1456 if (priv_ep->dequeue > priv_req->end_trb)
1459 if (priv_ep->dequeue < priv_req->start_trb)
1463 if ((priv_req->start_trb > priv_req->end_trb) &&
1464 (priv_ep->dequeue > priv_req->end_trb) &&
1465 (priv_ep->dequeue < priv_req->start_trb))
1468 if ((priv_req->start_trb == priv_req->end_trb) &&
1469 (priv_ep->dequeue != priv_req->end_trb))
1472 trb = &priv_ep->trb_pool[priv_ep->dequeue];
1474 if ((le32_to_cpu(trb->control) & TRB_CYCLE) != priv_ep->ccs)
1477 if (doorbell == 1 && current_index == priv_ep->dequeue)
1480 /* The corner case for TRBS_PER_SEGMENT equal 2). */
1481 if (TRBS_PER_SEGMENT == 2 && priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1486 if (priv_ep->enqueue == priv_ep->dequeue &&
1487 priv_ep->free_trbs == 0) {
1489 } else if (priv_ep->dequeue < current_index) {
1490 if ((current_index == (priv_ep->num_trbs - 1)) &&
1495 } else if (priv_ep->dequeue > current_index) {
1500 trace_cdns3_request_handled(priv_req, current_index, handled);
1505 static void cdns3_transfer_completed(struct cdns3_device *priv_dev,
1506 struct cdns3_endpoint *priv_ep)
1508 struct cdns3_request *priv_req;
1509 struct usb_request *request;
1510 struct cdns3_trb *trb;
1511 bool request_handled = false;
1512 bool transfer_end = false;
1514 while (!list_empty(&priv_ep->pending_req_list)) {
1515 request = cdns3_next_request(&priv_ep->pending_req_list);
1516 priv_req = to_cdns3_request(request);
1518 trb = priv_ep->trb_pool + priv_ep->dequeue;
1520 /* Request was dequeued and TRB was changed to TRB_LINK. */
1521 if (TRB_FIELD_TO_TYPE(le32_to_cpu(trb->control)) == TRB_LINK) {
1522 trace_cdns3_complete_trb(priv_ep, trb);
1523 cdns3_move_deq_to_next_trb(priv_req);
1526 if (!request->stream_id) {
1527 /* Re-select endpoint. It could be changed by other CPU
1528 * during handling usb_gadget_giveback_request.
1530 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1532 while (cdns3_trb_handled(priv_ep, priv_req)) {
1533 priv_req->finished_trb++;
1534 if (priv_req->finished_trb >= priv_req->num_of_trb)
1535 request_handled = true;
1537 trb = priv_ep->trb_pool + priv_ep->dequeue;
1538 trace_cdns3_complete_trb(priv_ep, trb);
1542 TRB_LEN(le32_to_cpu(trb->length));
1544 if (priv_req->num_of_trb > 1 &&
1545 le32_to_cpu(trb->control) & TRB_SMM)
1546 transfer_end = true;
1548 cdns3_ep_inc_deq(priv_ep);
1551 if (request_handled) {
1552 cdns3_gadget_giveback(priv_ep, priv_req, 0);
1553 request_handled = false;
1554 transfer_end = false;
1556 goto prepare_next_td;
1559 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC &&
1560 TRBS_PER_SEGMENT == 2)
1563 /* Re-select endpoint. It could be changed by other CPU
1564 * during handling usb_gadget_giveback_request.
1566 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1568 trb = priv_ep->trb_pool;
1569 trace_cdns3_complete_trb(priv_ep, trb);
1571 if (trb != priv_req->trb)
1572 dev_warn(priv_dev->dev,
1573 "request_trb=0x%p, queue_trb=0x%p\n",
1574 priv_req->trb, trb);
1576 request->actual += TRB_LEN(le32_to_cpu(trb->length));
1578 if (!request->num_sgs ||
1579 (request->num_sgs == (priv_ep->stream_sg_idx + 1))) {
1580 priv_ep->stream_sg_idx = 0;
1581 cdns3_gadget_giveback(priv_ep, priv_req, 0);
1583 priv_ep->stream_sg_idx++;
1584 cdns3_ep_run_stream_transfer(priv_ep, request);
1589 priv_ep->flags &= ~EP_PENDING_REQUEST;
1592 if (!(priv_ep->flags & EP_STALLED) &&
1593 !(priv_ep->flags & EP_STALL_PENDING))
1594 cdns3_start_all_request(priv_dev, priv_ep);
1597 void cdns3_rearm_transfer(struct cdns3_endpoint *priv_ep, u8 rearm)
1599 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1601 cdns3_wa1_restore_cycle_bit(priv_ep);
1604 trace_cdns3_ring(priv_ep);
1606 /* Cycle Bit must be updated before arming DMA. */
1608 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1610 __cdns3_gadget_wakeup(priv_dev);
1612 trace_cdns3_doorbell_epx(priv_ep->name,
1613 readl(&priv_dev->regs->ep_traddr));
1617 static void cdns3_reprogram_tdl(struct cdns3_endpoint *priv_ep)
1619 u16 tdl = priv_ep->pending_tdl;
1620 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1622 if (tdl > EP_CMD_TDL_MAX) {
1623 tdl = EP_CMD_TDL_MAX;
1624 priv_ep->pending_tdl -= EP_CMD_TDL_MAX;
1626 priv_ep->pending_tdl = 0;
1629 writel(EP_CMD_TDL_SET(tdl) | EP_CMD_STDL, &priv_dev->regs->ep_cmd);
1633 * cdns3_check_ep_interrupt_proceed - Processes interrupt related to endpoint
1634 * @priv_ep: endpoint object
1638 static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep)
1640 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1642 struct usb_request *deferred_request;
1643 struct usb_request *pending_request;
1646 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1648 trace_cdns3_epx_irq(priv_dev, priv_ep);
1650 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
1651 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
1653 if ((ep_sts_reg & EP_STS_PRIME) && priv_ep->use_streams) {
1654 bool dbusy = !!(ep_sts_reg & EP_STS_DBUSY);
1656 tdl = cdns3_get_tdl(priv_dev);
1659 * Continue the previous transfer:
1660 * There is some racing between ERDY and PRIME. The device send
1661 * ERDY and almost in the same time Host send PRIME. It cause
1662 * that host ignore the ERDY packet and driver has to send it
1665 if (tdl && (dbusy || !EP_STS_BUFFEMPTY(ep_sts_reg) ||
1666 EP_STS_HOSTPP(ep_sts_reg))) {
1667 writel(EP_CMD_ERDY |
1668 EP_CMD_ERDY_SID(priv_ep->last_stream_id),
1669 &priv_dev->regs->ep_cmd);
1670 ep_sts_reg &= ~(EP_STS_MD_EXIT | EP_STS_IOC);
1672 priv_ep->prime_flag = true;
1674 pending_request = cdns3_next_request(&priv_ep->pending_req_list);
1675 deferred_request = cdns3_next_request(&priv_ep->deferred_req_list);
1677 if (deferred_request && !pending_request) {
1678 cdns3_start_all_request(priv_dev, priv_ep);
1683 if (ep_sts_reg & EP_STS_TRBERR) {
1684 if (priv_ep->flags & EP_STALL_PENDING &&
1685 !(ep_sts_reg & EP_STS_DESCMIS &&
1686 priv_dev->dev_ver < DEV_VER_V2)) {
1687 cdns3_ep_stall_flush(priv_ep);
1691 * For isochronous transfer driver completes request on
1692 * IOC or on TRBERR. IOC appears only when device receive
1693 * OUT data packet. If host disable stream or lost some packet
1694 * then the only way to finish all queued transfer is to do it
1697 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC &&
1698 !priv_ep->wa1_set) {
1699 if (!priv_ep->dir) {
1700 u32 ep_cfg = readl(&priv_dev->regs->ep_cfg);
1702 ep_cfg &= ~EP_CFG_ENABLE;
1703 writel(ep_cfg, &priv_dev->regs->ep_cfg);
1704 priv_ep->flags &= ~EP_QUIRK_ISO_OUT_EN;
1706 cdns3_transfer_completed(priv_dev, priv_ep);
1707 } else if (!(priv_ep->flags & EP_STALLED) &&
1708 !(priv_ep->flags & EP_STALL_PENDING)) {
1709 if (priv_ep->flags & EP_DEFERRED_DRDY) {
1710 priv_ep->flags &= ~EP_DEFERRED_DRDY;
1711 cdns3_start_all_request(priv_dev, priv_ep);
1713 cdns3_rearm_transfer(priv_ep,
1719 if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP) ||
1720 (ep_sts_reg & EP_STS_IOT)) {
1721 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
1722 if (ep_sts_reg & EP_STS_ISP)
1723 priv_ep->flags |= EP_QUIRK_END_TRANSFER;
1725 priv_ep->flags &= ~EP_QUIRK_END_TRANSFER;
1728 if (!priv_ep->use_streams) {
1729 if ((ep_sts_reg & EP_STS_IOC) ||
1730 (ep_sts_reg & EP_STS_ISP)) {
1731 cdns3_transfer_completed(priv_dev, priv_ep);
1732 } else if ((priv_ep->flags & EP_TDLCHK_EN) &
1733 priv_ep->pending_tdl) {
1734 /* handle IOT with pending tdl */
1735 cdns3_reprogram_tdl(priv_ep);
1737 } else if (priv_ep->dir == USB_DIR_OUT) {
1738 priv_ep->ep_sts_pending |= ep_sts_reg;
1739 } else if (ep_sts_reg & EP_STS_IOT) {
1740 cdns3_transfer_completed(priv_dev, priv_ep);
1745 * MD_EXIT interrupt sets when stream capable endpoint exits
1746 * from MOVE DATA state of Bulk IN/OUT stream protocol state machine
1748 if (priv_ep->dir == USB_DIR_OUT && (ep_sts_reg & EP_STS_MD_EXIT) &&
1749 (priv_ep->ep_sts_pending & EP_STS_IOT) && priv_ep->use_streams) {
1750 priv_ep->ep_sts_pending = 0;
1751 cdns3_transfer_completed(priv_dev, priv_ep);
1755 * WA2: this condition should only be meet when
1756 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET or
1757 * priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN.
1758 * In other cases this interrupt will be disabled.
1760 if (ep_sts_reg & EP_STS_DESCMIS && priv_dev->dev_ver < DEV_VER_V2 &&
1761 !(priv_ep->flags & EP_STALLED))
1762 cdns3_wa2_descmissing_packet(priv_ep);
1767 static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev)
1769 if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect)
1770 priv_dev->gadget_driver->disconnect(&priv_dev->gadget);
1774 * cdns3_check_usb_interrupt_proceed - Processes interrupt related to device
1775 * @priv_dev: extended gadget object
1776 * @usb_ists: bitmap representation of device's reported interrupts
1777 * (usb_ists register value)
1779 static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev,
1781 __must_hold(&priv_dev->lock)
1785 trace_cdns3_usb_irq(priv_dev, usb_ists);
1786 if (usb_ists & USB_ISTS_L1ENTI) {
1788 * WORKAROUND: CDNS3 controller has issue with hardware resuming
1789 * from L1. To fix it, if any DMA transfer is pending driver
1790 * must starts driving resume signal immediately.
1792 if (readl(&priv_dev->regs->drbl))
1793 __cdns3_gadget_wakeup(priv_dev);
1796 /* Connection detected */
1797 if (usb_ists & (USB_ISTS_CON2I | USB_ISTS_CONI)) {
1798 speed = cdns3_get_speed(priv_dev);
1799 priv_dev->gadget.speed = speed;
1800 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_POWERED);
1801 cdns3_ep0_config(priv_dev);
1804 /* Disconnection detected */
1805 if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) {
1806 spin_unlock(&priv_dev->lock);
1807 cdns3_disconnect_gadget(priv_dev);
1808 spin_lock(&priv_dev->lock);
1809 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
1810 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
1811 cdns3_hw_reset_eps_config(priv_dev);
1814 if (usb_ists & (USB_ISTS_L2ENTI | USB_ISTS_U3ENTI)) {
1815 if (priv_dev->gadget_driver &&
1816 priv_dev->gadget_driver->suspend) {
1817 spin_unlock(&priv_dev->lock);
1818 priv_dev->gadget_driver->suspend(&priv_dev->gadget);
1819 spin_lock(&priv_dev->lock);
1823 if (usb_ists & (USB_ISTS_L2EXTI | USB_ISTS_U3EXTI)) {
1824 if (priv_dev->gadget_driver &&
1825 priv_dev->gadget_driver->resume) {
1826 spin_unlock(&priv_dev->lock);
1827 priv_dev->gadget_driver->resume(&priv_dev->gadget);
1828 spin_lock(&priv_dev->lock);
1833 if (usb_ists & (USB_ISTS_UWRESI | USB_ISTS_UHRESI | USB_ISTS_U2RESI)) {
1834 if (priv_dev->gadget_driver) {
1835 spin_unlock(&priv_dev->lock);
1836 usb_gadget_udc_reset(&priv_dev->gadget,
1837 priv_dev->gadget_driver);
1838 spin_lock(&priv_dev->lock);
1840 /*read again to check the actual speed*/
1841 speed = cdns3_get_speed(priv_dev);
1842 priv_dev->gadget.speed = speed;
1843 cdns3_hw_reset_eps_config(priv_dev);
1844 cdns3_ep0_config(priv_dev);
1850 * cdns3_device_irq_handler - interrupt handler for device part of controller
1852 * @irq: irq number for cdns3 core device
1853 * @data: structure of cdns3
1855 * Returns IRQ_HANDLED or IRQ_NONE
1857 static irqreturn_t cdns3_device_irq_handler(int irq, void *data)
1859 struct cdns3_device *priv_dev = data;
1860 struct cdns *cdns = dev_get_drvdata(priv_dev->dev);
1861 irqreturn_t ret = IRQ_NONE;
1867 /* check USB device interrupt */
1868 reg = readl(&priv_dev->regs->usb_ists);
1870 /* After masking interrupts the new interrupts won't be
1871 * reported in usb_ists/ep_ists. In order to not lose some
1872 * of them driver disables only detected interrupts.
1873 * They will be enabled ASAP after clearing source of
1874 * interrupt. This an unusual behavior only applies to
1875 * usb_ists register.
1877 reg = ~reg & readl(&priv_dev->regs->usb_ien);
1878 /* mask deferred interrupt. */
1879 writel(reg, &priv_dev->regs->usb_ien);
1880 ret = IRQ_WAKE_THREAD;
1883 /* check endpoint interrupt */
1884 reg = readl(&priv_dev->regs->ep_ists);
1886 writel(0, &priv_dev->regs->ep_ien);
1887 ret = IRQ_WAKE_THREAD;
1894 * cdns3_device_thread_irq_handler - interrupt handler for device part
1897 * @irq: irq number for cdns3 core device
1898 * @data: structure of cdns3
1900 * Returns IRQ_HANDLED or IRQ_NONE
1902 static irqreturn_t cdns3_device_thread_irq_handler(int irq, void *data)
1904 struct cdns3_device *priv_dev = data;
1905 irqreturn_t ret = IRQ_NONE;
1906 unsigned long flags;
1910 spin_lock_irqsave(&priv_dev->lock, flags);
1912 reg = readl(&priv_dev->regs->usb_ists);
1914 writel(reg, &priv_dev->regs->usb_ists);
1915 writel(USB_IEN_INIT, &priv_dev->regs->usb_ien);
1916 cdns3_check_usb_interrupt_proceed(priv_dev, reg);
1920 reg = readl(&priv_dev->regs->ep_ists);
1922 /* handle default endpoint OUT */
1923 if (reg & EP_ISTS_EP_OUT0) {
1924 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT);
1928 /* handle default endpoint IN */
1929 if (reg & EP_ISTS_EP_IN0) {
1930 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN);
1934 /* check if interrupt from non default endpoint, if no exit */
1935 reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0);
1939 for_each_set_bit(bit, ®,
1940 sizeof(u32) * BITS_PER_BYTE) {
1941 cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]);
1945 if (priv_dev->dev_ver < DEV_VER_V2 && priv_dev->using_streams)
1946 cdns3_wa2_check_outq_status(priv_dev);
1949 writel(~0, &priv_dev->regs->ep_ien);
1950 spin_unlock_irqrestore(&priv_dev->lock, flags);
1956 * cdns3_ep_onchip_buffer_reserve - Try to reserve onchip buf for EP
1958 * The real reservation will occur during write to EP_CFG register,
1959 * this function is used to check if the 'size' reservation is allowed.
1961 * @priv_dev: extended gadget object
1962 * @size: the size (KB) for EP would like to allocate
1963 * @is_in: endpoint direction
1965 * Return 0 if the required size can met or negative value on failure
1967 static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
1968 int size, int is_in)
1972 /* 2KB are reserved for EP0*/
1973 remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2;
1976 if (remained < size)
1979 priv_dev->onchip_used_size += size;
1984 * ALL OUT EPs are shared the same chunk onchip memory, so
1985 * driver checks if it already has assigned enough buffers
1987 if (priv_dev->out_mem_is_allocated >= size)
1990 required = size - priv_dev->out_mem_is_allocated;
1992 if (required > remained)
1995 priv_dev->out_mem_is_allocated += required;
1996 priv_dev->onchip_used_size += required;
2002 static void cdns3_configure_dmult(struct cdns3_device *priv_dev,
2003 struct cdns3_endpoint *priv_ep)
2005 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
2007 /* For dev_ver > DEV_VER_V2 DMULT is configured per endpoint */
2008 if (priv_dev->dev_ver <= DEV_VER_V2)
2009 writel(USB_CONF_DMULT, ®s->usb_conf);
2011 if (priv_dev->dev_ver == DEV_VER_V2)
2012 writel(USB_CONF2_EN_TDL_TRB, ®s->usb_conf2);
2014 if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) {
2018 mask = BIT(priv_ep->num + 16);
2020 mask = BIT(priv_ep->num);
2022 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
2023 cdns3_set_register_bit(®s->tdl_from_trb, mask);
2024 cdns3_set_register_bit(®s->tdl_beh, mask);
2025 cdns3_set_register_bit(®s->tdl_beh2, mask);
2026 cdns3_set_register_bit(®s->dma_adv_td, mask);
2029 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2030 cdns3_set_register_bit(®s->tdl_from_trb, mask);
2032 cdns3_set_register_bit(®s->dtrans, mask);
2037 * cdns3_ep_config - Configure hardware endpoint
2038 * @priv_ep: extended endpoint object
2039 * @enable: set EP_CFG_ENABLE bit in ep_cfg register.
2041 int cdns3_ep_config(struct cdns3_endpoint *priv_ep, bool enable)
2043 bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
2044 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2045 u32 bEndpointAddress = priv_ep->num | priv_ep->dir;
2046 u32 max_packet_size = 0;
2053 buffering = CDNS3_EP_BUF_SIZE - 1;
2055 cdns3_configure_dmult(priv_dev, priv_ep);
2057 switch (priv_ep->type) {
2058 case USB_ENDPOINT_XFER_INT:
2059 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT);
2061 if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir)
2062 ep_cfg |= EP_CFG_TDL_CHK;
2064 case USB_ENDPOINT_XFER_BULK:
2065 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK);
2067 if (priv_dev->dev_ver >= DEV_VER_V2 && !priv_ep->dir)
2068 ep_cfg |= EP_CFG_TDL_CHK;
2071 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC);
2072 mult = CDNS3_EP_ISO_HS_MULT - 1;
2073 buffering = mult + 1;
2076 switch (priv_dev->gadget.speed) {
2077 case USB_SPEED_FULL:
2078 max_packet_size = is_iso_ep ? 1023 : 64;
2080 case USB_SPEED_HIGH:
2081 max_packet_size = is_iso_ep ? 1024 : 512;
2083 case USB_SPEED_SUPER:
2084 /* It's limitation that driver assumes in driver. */
2086 max_packet_size = 1024;
2087 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2088 maxburst = CDNS3_EP_ISO_SS_BURST - 1;
2089 buffering = (mult + 1) *
2092 if (priv_ep->interval > 1)
2095 maxburst = CDNS3_EP_BUF_SIZE - 1;
2099 /* all other speed are not supported */
2103 if (max_packet_size == 1024)
2104 priv_ep->trb_burst_size = 128;
2105 else if (max_packet_size >= 512)
2106 priv_ep->trb_burst_size = 64;
2108 priv_ep->trb_burst_size = 16;
2110 /* onchip buffer is only allocated before configuration */
2111 if (!priv_dev->hw_configured_flag) {
2112 ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
2115 dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
2121 ep_cfg |= EP_CFG_ENABLE;
2123 if (priv_ep->use_streams && priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2124 if (priv_dev->dev_ver >= DEV_VER_V3) {
2125 u32 mask = BIT(priv_ep->num + (priv_ep->dir ? 16 : 0));
2128 * Stream capable endpoints are handled by using ep_tdl
2129 * register. Other endpoints use TDL from TRB feature.
2131 cdns3_clear_register_bit(&priv_dev->regs->tdl_from_trb,
2135 /* Enable Stream Bit TDL chk and SID chk */
2136 ep_cfg |= EP_CFG_STREAM_EN | EP_CFG_TDL_CHK | EP_CFG_SID_CHK;
2139 ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
2141 EP_CFG_BUFFERING(buffering) |
2142 EP_CFG_MAXBURST(maxburst);
2144 cdns3_select_ep(priv_dev, bEndpointAddress);
2145 writel(ep_cfg, &priv_dev->regs->ep_cfg);
2146 priv_ep->flags |= EP_CONFIGURED;
2148 dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
2149 priv_ep->name, ep_cfg);
2154 /* Find correct direction for HW endpoint according to description */
2155 static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc,
2156 struct cdns3_endpoint *priv_ep)
2158 return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) ||
2159 (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc));
2163 cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev,
2164 struct usb_endpoint_descriptor *desc)
2167 struct cdns3_endpoint *priv_ep;
2169 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2172 /* ep name pattern likes epXin or epXout */
2173 char c[2] = {ep->name[2], '\0'};
2175 ret = kstrtoul(c, 10, &num);
2177 return ERR_PTR(ret);
2179 priv_ep = ep_to_cdns3_ep(ep);
2180 if (cdns3_ep_dir_is_correct(desc, priv_ep)) {
2181 if (!(priv_ep->flags & EP_CLAIMED)) {
2188 return ERR_PTR(-ENOENT);
2192 * Cadence IP has one limitation that all endpoints must be configured
2193 * (Type & MaxPacketSize) before setting configuration through hardware
2194 * register, it means we can't change endpoints configuration after
2195 * set_configuration.
2197 * This function set EP_CLAIMED flag which is added when the gadget driver
2198 * uses usb_ep_autoconfig to configure specific endpoint;
2199 * When the udc driver receives set_configurion request,
2200 * it goes through all claimed endpoints, and configure all endpoints
2203 * At usb_ep_ops.enable/disable, we only enable and disable endpoint through
2204 * ep_cfg register which can be changed after set_configuration, and do
2205 * some software operation accordingly.
2208 usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget,
2209 struct usb_endpoint_descriptor *desc,
2210 struct usb_ss_ep_comp_descriptor *comp_desc)
2212 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2213 struct cdns3_endpoint *priv_ep;
2214 unsigned long flags;
2216 priv_ep = cdns3_find_available_ep(priv_dev, desc);
2217 if (IS_ERR(priv_ep)) {
2218 dev_err(priv_dev->dev, "no available ep\n");
2222 dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name);
2224 spin_lock_irqsave(&priv_dev->lock, flags);
2225 priv_ep->endpoint.desc = desc;
2226 priv_ep->dir = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT;
2227 priv_ep->type = usb_endpoint_type(desc);
2228 priv_ep->flags |= EP_CLAIMED;
2229 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2231 spin_unlock_irqrestore(&priv_dev->lock, flags);
2232 return &priv_ep->endpoint;
2236 * cdns3_gadget_ep_alloc_request - Allocates request
2237 * @ep: endpoint object associated with request
2238 * @gfp_flags: gfp flags
2240 * Returns allocated request address, NULL on allocation error
2242 struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep,
2245 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2246 struct cdns3_request *priv_req;
2248 priv_req = kzalloc(sizeof(*priv_req), gfp_flags);
2252 priv_req->priv_ep = priv_ep;
2254 trace_cdns3_alloc_request(priv_req);
2255 return &priv_req->request;
2259 * cdns3_gadget_ep_free_request - Free memory occupied by request
2260 * @ep: endpoint object associated with request
2261 * @request: request to free memory
2263 void cdns3_gadget_ep_free_request(struct usb_ep *ep,
2264 struct usb_request *request)
2266 struct cdns3_request *priv_req = to_cdns3_request(request);
2268 if (priv_req->aligned_buf)
2269 priv_req->aligned_buf->in_use = 0;
2271 trace_cdns3_free_request(priv_req);
2276 * cdns3_gadget_ep_enable - Enable endpoint
2277 * @ep: endpoint object
2278 * @desc: endpoint descriptor
2280 * Returns 0 on success, error code elsewhere
2282 static int cdns3_gadget_ep_enable(struct usb_ep *ep,
2283 const struct usb_endpoint_descriptor *desc)
2285 struct cdns3_endpoint *priv_ep;
2286 struct cdns3_device *priv_dev;
2287 const struct usb_ss_ep_comp_descriptor *comp_desc;
2288 u32 reg = EP_STS_EN_TRBERREN;
2289 u32 bEndpointAddress;
2290 unsigned long flags;
2295 priv_ep = ep_to_cdns3_ep(ep);
2296 priv_dev = priv_ep->cdns3_dev;
2297 comp_desc = priv_ep->endpoint.comp_desc;
2299 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
2300 dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
2304 if (!desc->wMaxPacketSize) {
2305 dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
2309 if (dev_WARN_ONCE(priv_dev->dev, priv_ep->flags & EP_ENABLED,
2310 "%s is already enabled\n", priv_ep->name))
2313 spin_lock_irqsave(&priv_dev->lock, flags);
2315 priv_ep->endpoint.desc = desc;
2316 priv_ep->type = usb_endpoint_type(desc);
2317 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
2319 if (priv_ep->interval > ISO_MAX_INTERVAL &&
2320 priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
2321 dev_err(priv_dev->dev, "Driver is limited to %d period\n",
2328 bEndpointAddress = priv_ep->num | priv_ep->dir;
2329 cdns3_select_ep(priv_dev, bEndpointAddress);
2332 * For some versions of controller at some point during ISO OUT traffic
2333 * DMA reads Transfer Ring for the EP which has never got doorbell.
2334 * This issue was detected only on simulation, but to avoid this issue
2335 * driver add protection against it. To fix it driver enable ISO OUT
2336 * endpoint before setting DRBL. This special treatment of ISO OUT
2337 * endpoints are recommended by controller specification.
2339 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
2342 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
2344 * Enable stream support (SS mode) related interrupts
2345 * in EP_STS_EN Register
2347 if (priv_dev->gadget.speed >= USB_SPEED_SUPER) {
2348 reg |= EP_STS_EN_IOTEN | EP_STS_EN_PRIMEEEN |
2349 EP_STS_EN_SIDERREN | EP_STS_EN_MD_EXITEN |
2350 EP_STS_EN_STREAMREN;
2351 priv_ep->use_streams = true;
2352 ret = cdns3_ep_config(priv_ep, enable);
2353 priv_dev->using_streams |= true;
2356 ret = cdns3_ep_config(priv_ep, enable);
2362 ret = cdns3_allocate_trb_pool(priv_ep);
2366 bEndpointAddress = priv_ep->num | priv_ep->dir;
2367 cdns3_select_ep(priv_dev, bEndpointAddress);
2369 trace_cdns3_gadget_ep_enable(priv_ep);
2371 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2373 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2374 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2377 if (unlikely(ret)) {
2378 cdns3_free_trb_pool(priv_ep);
2383 /* enable interrupt for selected endpoint */
2384 cdns3_set_register_bit(&priv_dev->regs->ep_ien,
2385 BIT(cdns3_ep_addr_to_index(bEndpointAddress)));
2387 if (priv_dev->dev_ver < DEV_VER_V2)
2388 cdns3_wa2_enable_detection(priv_dev, priv_ep, reg);
2390 writel(reg, &priv_dev->regs->ep_sts_en);
2393 priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING |
2394 EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN);
2395 priv_ep->flags |= EP_ENABLED | EP_UPDATE_EP_TRBADDR;
2396 priv_ep->wa1_set = 0;
2397 priv_ep->enqueue = 0;
2398 priv_ep->dequeue = 0;
2399 reg = readl(&priv_dev->regs->ep_sts);
2400 priv_ep->pcs = !!EP_STS_CCS(reg);
2401 priv_ep->ccs = !!EP_STS_CCS(reg);
2402 /* one TRB is reserved for link TRB used in DMULT mode*/
2403 priv_ep->free_trbs = priv_ep->num_trbs - 1;
2405 spin_unlock_irqrestore(&priv_dev->lock, flags);
2411 * cdns3_gadget_ep_disable - Disable endpoint
2412 * @ep: endpoint object
2414 * Returns 0 on success, error code elsewhere
2416 static int cdns3_gadget_ep_disable(struct usb_ep *ep)
2418 struct cdns3_endpoint *priv_ep;
2419 struct cdns3_request *priv_req;
2420 struct cdns3_device *priv_dev;
2421 struct usb_request *request;
2422 unsigned long flags;
2428 pr_err("usbss: invalid parameters\n");
2432 priv_ep = ep_to_cdns3_ep(ep);
2433 priv_dev = priv_ep->cdns3_dev;
2435 if (dev_WARN_ONCE(priv_dev->dev, !(priv_ep->flags & EP_ENABLED),
2436 "%s is already disabled\n", priv_ep->name))
2439 spin_lock_irqsave(&priv_dev->lock, flags);
2441 trace_cdns3_gadget_ep_disable(priv_ep);
2443 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2445 ep_cfg = readl(&priv_dev->regs->ep_cfg);
2446 ep_cfg &= ~EP_CFG_ENABLE;
2447 writel(ep_cfg, &priv_dev->regs->ep_cfg);
2450 * Driver needs some time before resetting endpoint.
2451 * It need waits for clearing DBUSY bit or for timeout expired.
2452 * 10us is enough time for controller to stop transfer.
2454 readl_poll_timeout_atomic(&priv_dev->regs->ep_sts, val,
2455 !(val & EP_STS_DBUSY), 1, 10);
2456 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2458 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2459 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
2462 dev_err(priv_dev->dev, "Timeout: %s resetting failed.\n",
2465 while (!list_empty(&priv_ep->pending_req_list)) {
2466 request = cdns3_next_request(&priv_ep->pending_req_list);
2468 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
2472 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
2473 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
2475 kfree(priv_req->request.buf);
2476 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
2477 &priv_req->request);
2478 list_del_init(&priv_req->list);
2479 --priv_ep->wa2_counter;
2482 while (!list_empty(&priv_ep->deferred_req_list)) {
2483 request = cdns3_next_request(&priv_ep->deferred_req_list);
2485 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
2489 priv_ep->descmis_req = NULL;
2492 priv_ep->flags &= ~EP_ENABLED;
2493 priv_ep->use_streams = false;
2495 spin_unlock_irqrestore(&priv_dev->lock, flags);
2501 * __cdns3_gadget_ep_queue - Transfer data on endpoint
2502 * @ep: endpoint object
2503 * @request: request object
2504 * @gfp_flags: gfp flags
2506 * Returns 0 on success, error code elsewhere
2508 static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
2509 struct usb_request *request,
2512 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2513 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2514 struct cdns3_request *priv_req;
2517 request->actual = 0;
2518 request->status = -EINPROGRESS;
2519 priv_req = to_cdns3_request(request);
2520 trace_cdns3_ep_queue(priv_req);
2522 if (priv_dev->dev_ver < DEV_VER_V2) {
2523 ret = cdns3_wa2_gadget_ep_queue(priv_dev, priv_ep,
2526 if (ret == EINPROGRESS)
2530 ret = cdns3_prepare_aligned_request_buf(priv_req);
2534 ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request,
2535 usb_endpoint_dir_in(ep->desc));
2539 list_add_tail(&request->list, &priv_ep->deferred_req_list);
2542 * For stream capable endpoint if prime irq flag is set then only start
2544 * If hardware endpoint configuration has not been set yet then
2545 * just queue request in deferred list. Transfer will be started in
2546 * cdns3_set_hw_configuration.
2548 if (!request->stream_id) {
2549 if (priv_dev->hw_configured_flag &&
2550 !(priv_ep->flags & EP_STALLED) &&
2551 !(priv_ep->flags & EP_STALL_PENDING))
2552 cdns3_start_all_request(priv_dev, priv_ep);
2554 if (priv_dev->hw_configured_flag && priv_ep->prime_flag)
2555 cdns3_start_all_request(priv_dev, priv_ep);
2561 static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
2564 struct usb_request *zlp_request;
2565 struct cdns3_endpoint *priv_ep;
2566 struct cdns3_device *priv_dev;
2567 unsigned long flags;
2570 if (!request || !ep)
2573 priv_ep = ep_to_cdns3_ep(ep);
2574 priv_dev = priv_ep->cdns3_dev;
2576 spin_lock_irqsave(&priv_dev->lock, flags);
2578 ret = __cdns3_gadget_ep_queue(ep, request, gfp_flags);
2580 if (ret == 0 && request->zero && request->length &&
2581 (request->length % ep->maxpacket == 0)) {
2582 struct cdns3_request *priv_req;
2584 zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
2585 zlp_request->buf = priv_dev->zlp_buf;
2586 zlp_request->length = 0;
2588 priv_req = to_cdns3_request(zlp_request);
2589 priv_req->flags |= REQUEST_ZLP;
2591 dev_dbg(priv_dev->dev, "Queuing ZLP for endpoint: %s\n",
2593 ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags);
2596 spin_unlock_irqrestore(&priv_dev->lock, flags);
2601 * cdns3_gadget_ep_dequeue - Remove request from transfer queue
2602 * @ep: endpoint object associated with request
2603 * @request: request object
2605 * Returns 0 on success, error code elsewhere
2607 int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
2608 struct usb_request *request)
2610 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2611 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2612 struct usb_request *req, *req_temp;
2613 struct cdns3_request *priv_req;
2614 struct cdns3_trb *link_trb;
2615 u8 req_on_hw_ring = 0;
2616 unsigned long flags;
2619 if (!ep || !request || !ep->desc)
2622 spin_lock_irqsave(&priv_dev->lock, flags);
2624 priv_req = to_cdns3_request(request);
2626 trace_cdns3_ep_dequeue(priv_req);
2628 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2630 list_for_each_entry_safe(req, req_temp, &priv_ep->pending_req_list,
2632 if (request == req) {
2638 list_for_each_entry_safe(req, req_temp, &priv_ep->deferred_req_list,
2647 link_trb = priv_req->trb;
2649 /* Update ring only if removed request is on pending_req_list list */
2650 if (req_on_hw_ring && link_trb) {
2651 link_trb->buffer = cpu_to_le32(TRB_BUFFER(priv_ep->trb_pool_dma +
2652 ((priv_req->end_trb + 1) * TRB_SIZE)));
2653 link_trb->control = cpu_to_le32((le32_to_cpu(link_trb->control) & TRB_CYCLE) |
2654 TRB_TYPE(TRB_LINK) | TRB_CHAIN);
2656 if (priv_ep->wa1_trb == priv_req->trb)
2657 cdns3_wa1_restore_cycle_bit(priv_ep);
2660 cdns3_gadget_giveback(priv_ep, priv_req, -ECONNRESET);
2663 spin_unlock_irqrestore(&priv_dev->lock, flags);
2668 * __cdns3_gadget_ep_set_halt - Sets stall on selected endpoint
2669 * Should be called after acquiring spin_lock and selecting ep
2670 * @priv_ep: endpoint object to set stall on.
2672 void __cdns3_gadget_ep_set_halt(struct cdns3_endpoint *priv_ep)
2674 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2676 trace_cdns3_halt(priv_ep, 1, 0);
2678 if (!(priv_ep->flags & EP_STALLED)) {
2679 u32 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
2681 if (!(ep_sts_reg & EP_STS_DBUSY))
2682 cdns3_ep_stall_flush(priv_ep);
2684 priv_ep->flags |= EP_STALL_PENDING;
2689 * __cdns3_gadget_ep_clear_halt - Clears stall on selected endpoint
2690 * Should be called after acquiring spin_lock and selecting ep
2691 * @priv_ep: endpoint object to clear stall on
2693 int __cdns3_gadget_ep_clear_halt(struct cdns3_endpoint *priv_ep)
2695 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2696 struct usb_request *request;
2697 struct cdns3_request *priv_req;
2698 struct cdns3_trb *trb = NULL;
2702 trace_cdns3_halt(priv_ep, 0, 0);
2704 request = cdns3_next_request(&priv_ep->pending_req_list);
2706 priv_req = to_cdns3_request(request);
2707 trb = priv_req->trb;
2709 trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE);
2712 writel(EP_CMD_CSTALL | EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2714 /* wait for EPRST cleared */
2715 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2716 !(val & EP_CMD_EPRST), 1, 100);
2720 priv_ep->flags &= ~(EP_STALLED | EP_STALL_PENDING);
2724 trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE);
2726 cdns3_rearm_transfer(priv_ep, 1);
2729 cdns3_start_all_request(priv_dev, priv_ep);
2734 * cdns3_gadget_ep_set_halt - Sets/clears stall on selected endpoint
2735 * @ep: endpoint object to set/clear stall on
2736 * @value: 1 for set stall, 0 for clear stall
2738 * Returns 0 on success, error code elsewhere
2740 int cdns3_gadget_ep_set_halt(struct usb_ep *ep, int value)
2742 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2743 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2744 unsigned long flags;
2747 if (!(priv_ep->flags & EP_ENABLED))
2750 spin_lock_irqsave(&priv_dev->lock, flags);
2752 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2755 priv_ep->flags &= ~EP_WEDGE;
2756 ret = __cdns3_gadget_ep_clear_halt(priv_ep);
2758 __cdns3_gadget_ep_set_halt(priv_ep);
2761 spin_unlock_irqrestore(&priv_dev->lock, flags);
2766 extern const struct usb_ep_ops cdns3_gadget_ep0_ops;
2768 static const struct usb_ep_ops cdns3_gadget_ep_ops = {
2769 .enable = cdns3_gadget_ep_enable,
2770 .disable = cdns3_gadget_ep_disable,
2771 .alloc_request = cdns3_gadget_ep_alloc_request,
2772 .free_request = cdns3_gadget_ep_free_request,
2773 .queue = cdns3_gadget_ep_queue,
2774 .dequeue = cdns3_gadget_ep_dequeue,
2775 .set_halt = cdns3_gadget_ep_set_halt,
2776 .set_wedge = cdns3_gadget_ep_set_wedge,
2780 * cdns3_gadget_get_frame - Returns number of actual ITP frame
2781 * @gadget: gadget object
2783 * Returns number of actual ITP frame
2785 static int cdns3_gadget_get_frame(struct usb_gadget *gadget)
2787 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2789 return readl(&priv_dev->regs->usb_itpn);
2792 int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev)
2794 enum usb_device_speed speed;
2796 speed = cdns3_get_speed(priv_dev);
2798 if (speed >= USB_SPEED_SUPER)
2801 /* Start driving resume signaling to indicate remote wakeup. */
2802 writel(USB_CONF_LGO_L0, &priv_dev->regs->usb_conf);
2807 static int cdns3_gadget_wakeup(struct usb_gadget *gadget)
2809 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2810 unsigned long flags;
2813 spin_lock_irqsave(&priv_dev->lock, flags);
2814 ret = __cdns3_gadget_wakeup(priv_dev);
2815 spin_unlock_irqrestore(&priv_dev->lock, flags);
2819 static int cdns3_gadget_set_selfpowered(struct usb_gadget *gadget,
2822 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2823 unsigned long flags;
2825 spin_lock_irqsave(&priv_dev->lock, flags);
2826 priv_dev->is_selfpowered = !!is_selfpowered;
2827 spin_unlock_irqrestore(&priv_dev->lock, flags);
2831 static int cdns3_gadget_pullup(struct usb_gadget *gadget, int is_on)
2833 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2836 writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
2838 writel(~0, &priv_dev->regs->ep_ists);
2839 writel(~0, &priv_dev->regs->usb_ists);
2840 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
2846 static void cdns3_gadget_config(struct cdns3_device *priv_dev)
2848 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
2851 cdns3_ep0_config(priv_dev);
2853 /* enable interrupts for endpoint 0 (in and out) */
2854 writel(EP_IEN_EP_OUT0 | EP_IEN_EP_IN0, ®s->ep_ien);
2857 * Driver needs to modify LFPS minimal U1 Exit time for DEV_VER_TI_V1
2858 * revision of controller.
2860 if (priv_dev->dev_ver == DEV_VER_TI_V1) {
2861 reg = readl(®s->dbg_link1);
2863 reg &= ~DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_MASK;
2864 reg |= DBG_LINK1_LFPS_MIN_GEN_U1_EXIT(0x55) |
2865 DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_SET;
2866 writel(reg, ®s->dbg_link1);
2870 * By default some platforms has set protected access to memory.
2871 * This cause problem with cache, so driver restore non-secure
2874 reg = readl(®s->dma_axi_ctrl);
2875 reg |= DMA_AXI_CTRL_MARPROT(DMA_AXI_CTRL_NON_SECURE) |
2876 DMA_AXI_CTRL_MAWPROT(DMA_AXI_CTRL_NON_SECURE);
2877 writel(reg, ®s->dma_axi_ctrl);
2879 /* enable generic interrupt*/
2880 writel(USB_IEN_INIT, ®s->usb_ien);
2881 writel(USB_CONF_CLK2OFFDS | USB_CONF_L1DS, ®s->usb_conf);
2882 /* keep Fast Access bit */
2883 writel(PUSB_PWR_FST_REG_ACCESS, &priv_dev->regs->usb_pwr);
2885 cdns3_configure_dmult(priv_dev, NULL);
2889 * cdns3_gadget_udc_start - Gadget start
2890 * @gadget: gadget object
2891 * @driver: driver which operates on this gadget
2893 * Returns 0 on success, error code elsewhere
2895 static int cdns3_gadget_udc_start(struct usb_gadget *gadget,
2896 struct usb_gadget_driver *driver)
2898 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2899 unsigned long flags;
2900 enum usb_device_speed max_speed = driver->max_speed;
2902 spin_lock_irqsave(&priv_dev->lock, flags);
2903 priv_dev->gadget_driver = driver;
2905 /* limit speed if necessary */
2906 max_speed = min(driver->max_speed, gadget->max_speed);
2908 switch (max_speed) {
2909 case USB_SPEED_FULL:
2910 writel(USB_CONF_SFORCE_FS, &priv_dev->regs->usb_conf);
2911 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2913 case USB_SPEED_HIGH:
2914 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2916 case USB_SPEED_SUPER:
2919 dev_err(priv_dev->dev,
2920 "invalid maximum_speed parameter %d\n",
2923 case USB_SPEED_UNKNOWN:
2924 /* default to superspeed */
2925 max_speed = USB_SPEED_SUPER;
2929 cdns3_gadget_config(priv_dev);
2930 spin_unlock_irqrestore(&priv_dev->lock, flags);
2935 * cdns3_gadget_udc_stop - Stops gadget
2936 * @gadget: gadget object
2940 static int cdns3_gadget_udc_stop(struct usb_gadget *gadget)
2942 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2943 struct cdns3_endpoint *priv_ep;
2944 u32 bEndpointAddress;
2948 priv_dev->gadget_driver = NULL;
2950 priv_dev->onchip_used_size = 0;
2951 priv_dev->out_mem_is_allocated = 0;
2952 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
2954 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2955 priv_ep = ep_to_cdns3_ep(ep);
2956 bEndpointAddress = priv_ep->num | priv_ep->dir;
2957 cdns3_select_ep(priv_dev, bEndpointAddress);
2958 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2959 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2960 !(val & EP_CMD_EPRST), 1, 100);
2962 priv_ep->flags &= ~EP_CLAIMED;
2965 /* disable interrupt for device */
2966 writel(0, &priv_dev->regs->usb_ien);
2967 writel(0, &priv_dev->regs->usb_pwr);
2968 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
2973 static const struct usb_gadget_ops cdns3_gadget_ops = {
2974 .get_frame = cdns3_gadget_get_frame,
2975 .wakeup = cdns3_gadget_wakeup,
2976 .set_selfpowered = cdns3_gadget_set_selfpowered,
2977 .pullup = cdns3_gadget_pullup,
2978 .udc_start = cdns3_gadget_udc_start,
2979 .udc_stop = cdns3_gadget_udc_stop,
2980 .match_ep = cdns3_gadget_match_ep,
2983 static void cdns3_free_all_eps(struct cdns3_device *priv_dev)
2987 /* ep0 OUT point to ep0 IN. */
2988 priv_dev->eps[16] = NULL;
2990 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
2991 if (priv_dev->eps[i]) {
2992 cdns3_free_trb_pool(priv_dev->eps[i]);
2993 devm_kfree(priv_dev->dev, priv_dev->eps[i]);
2998 * cdns3_init_eps - Initializes software endpoints of gadget
2999 * @priv_dev: extended gadget object
3001 * Returns 0 on success, error code elsewhere
3003 static int cdns3_init_eps(struct cdns3_device *priv_dev)
3005 u32 ep_enabled_reg, iso_ep_reg;
3006 struct cdns3_endpoint *priv_ep;
3007 int ep_dir, ep_number;
3012 /* Read it from USB_CAP3 to USB_CAP5 */
3013 ep_enabled_reg = readl(&priv_dev->regs->usb_cap3);
3014 iso_ep_reg = readl(&priv_dev->regs->usb_cap4);
3016 dev_dbg(priv_dev->dev, "Initializing non-zero endpoints\n");
3018 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
3019 ep_dir = i >> 4; /* i div 16 */
3020 ep_number = i & 0xF; /* i % 16 */
3023 if (!(ep_enabled_reg & ep_mask))
3026 if (ep_dir && !ep_number) {
3027 priv_dev->eps[i] = priv_dev->eps[0];
3031 priv_ep = devm_kzalloc(priv_dev->dev, sizeof(*priv_ep),
3036 /* set parent of endpoint object */
3037 priv_ep->cdns3_dev = priv_dev;
3038 priv_dev->eps[i] = priv_ep;
3039 priv_ep->num = ep_number;
3040 priv_ep->dir = ep_dir ? USB_DIR_IN : USB_DIR_OUT;
3043 ret = cdns3_init_ep0(priv_dev, priv_ep);
3045 dev_err(priv_dev->dev, "Failed to init ep0\n");
3049 snprintf(priv_ep->name, sizeof(priv_ep->name), "ep%d%s",
3050 ep_number, !!ep_dir ? "in" : "out");
3051 priv_ep->endpoint.name = priv_ep->name;
3053 usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
3054 CDNS3_EP_MAX_PACKET_LIMIT);
3055 priv_ep->endpoint.max_streams = CDNS3_EP_MAX_STREAMS;
3056 priv_ep->endpoint.ops = &cdns3_gadget_ep_ops;
3058 priv_ep->endpoint.caps.dir_in = 1;
3060 priv_ep->endpoint.caps.dir_out = 1;
3062 if (iso_ep_reg & ep_mask)
3063 priv_ep->endpoint.caps.type_iso = 1;
3065 priv_ep->endpoint.caps.type_bulk = 1;
3066 priv_ep->endpoint.caps.type_int = 1;
3068 list_add_tail(&priv_ep->endpoint.ep_list,
3069 &priv_dev->gadget.ep_list);
3074 dev_dbg(priv_dev->dev, "Initialized %s support: %s %s\n",
3076 priv_ep->endpoint.caps.type_bulk ? "BULK, INT" : "",
3077 priv_ep->endpoint.caps.type_iso ? "ISO" : "");
3079 INIT_LIST_HEAD(&priv_ep->pending_req_list);
3080 INIT_LIST_HEAD(&priv_ep->deferred_req_list);
3081 INIT_LIST_HEAD(&priv_ep->wa2_descmiss_req_list);
3086 cdns3_free_all_eps(priv_dev);
3090 static void cdns3_gadget_release(struct device *dev)
3092 struct cdns3_device *priv_dev = container_of(dev,
3093 struct cdns3_device, gadget.dev);
3098 static void cdns3_gadget_exit(struct cdns *cdns)
3100 struct cdns3_device *priv_dev;
3102 priv_dev = cdns->gadget_dev;
3105 pm_runtime_mark_last_busy(cdns->dev);
3106 pm_runtime_put_autosuspend(cdns->dev);
3108 usb_del_gadget(&priv_dev->gadget);
3109 devm_free_irq(cdns->dev, cdns->dev_irq, priv_dev);
3111 cdns3_free_all_eps(priv_dev);
3113 while (!list_empty(&priv_dev->aligned_buf_list)) {
3114 struct cdns3_aligned_buf *buf;
3116 buf = cdns3_next_align_buf(&priv_dev->aligned_buf_list);
3117 dma_free_noncoherent(priv_dev->sysdev, buf->size,
3122 list_del(&buf->list);
3126 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3127 priv_dev->setup_dma);
3128 dma_pool_destroy(priv_dev->eps_dma_pool);
3130 kfree(priv_dev->zlp_buf);
3131 usb_put_gadget(&priv_dev->gadget);
3132 cdns->gadget_dev = NULL;
3133 cdns_drd_gadget_off(cdns);
3136 static int cdns3_gadget_start(struct cdns *cdns)
3138 struct cdns3_device *priv_dev;
3142 priv_dev = kzalloc(sizeof(*priv_dev), GFP_KERNEL);
3146 usb_initialize_gadget(cdns->dev, &priv_dev->gadget,
3147 cdns3_gadget_release);
3148 cdns->gadget_dev = priv_dev;
3149 priv_dev->sysdev = cdns->dev;
3150 priv_dev->dev = cdns->dev;
3151 priv_dev->regs = cdns->dev_regs;
3153 device_property_read_u16(priv_dev->dev, "cdns,on-chip-buff-size",
3154 &priv_dev->onchip_buffers);
3156 if (priv_dev->onchip_buffers <= 0) {
3157 u32 reg = readl(&priv_dev->regs->usb_cap2);
3159 priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg);
3162 if (!priv_dev->onchip_buffers)
3163 priv_dev->onchip_buffers = 256;
3165 max_speed = usb_get_maximum_speed(cdns->dev);
3167 /* Check the maximum_speed parameter */
3168 switch (max_speed) {
3169 case USB_SPEED_FULL:
3170 case USB_SPEED_HIGH:
3171 case USB_SPEED_SUPER:
3174 dev_err(cdns->dev, "invalid maximum_speed parameter %d\n",
3177 case USB_SPEED_UNKNOWN:
3178 /* default to superspeed */
3179 max_speed = USB_SPEED_SUPER;
3183 /* fill gadget fields */
3184 priv_dev->gadget.max_speed = max_speed;
3185 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3186 priv_dev->gadget.ops = &cdns3_gadget_ops;
3187 priv_dev->gadget.name = "usb-ss-gadget";
3188 priv_dev->gadget.quirk_avoids_skb_reserve = 1;
3189 priv_dev->gadget.irq = cdns->dev_irq;
3191 spin_lock_init(&priv_dev->lock);
3192 INIT_WORK(&priv_dev->pending_status_wq,
3193 cdns3_pending_setup_status_handler);
3195 INIT_WORK(&priv_dev->aligned_buf_wq,
3196 cdns3_free_aligned_request_buf);
3198 /* initialize endpoint container */
3199 INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
3200 INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
3201 priv_dev->eps_dma_pool = dma_pool_create("cdns3_eps_dma_pool",
3203 TRB_RING_SIZE, 8, 0);
3204 if (!priv_dev->eps_dma_pool) {
3205 dev_err(priv_dev->dev, "Failed to create TRB dma pool\n");
3210 ret = cdns3_init_eps(priv_dev);
3212 dev_err(priv_dev->dev, "Failed to create endpoints\n");
3216 /* allocate memory for setup packet buffer */
3217 priv_dev->setup_buf = dma_alloc_coherent(priv_dev->sysdev, 8,
3218 &priv_dev->setup_dma, GFP_DMA);
3219 if (!priv_dev->setup_buf) {
3224 priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6);
3226 dev_dbg(priv_dev->dev, "Device Controller version: %08x\n",
3227 readl(&priv_dev->regs->usb_cap6));
3228 dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n",
3229 readl(&priv_dev->regs->usb_cap1));
3230 dev_dbg(priv_dev->dev, "On-Chip memory configuration: %08x\n",
3231 readl(&priv_dev->regs->usb_cap2));
3233 priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver);
3234 if (priv_dev->dev_ver >= DEV_VER_V2)
3235 priv_dev->gadget.sg_supported = 1;
3237 priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL);
3238 if (!priv_dev->zlp_buf) {
3243 /* add USB gadget device */
3244 ret = usb_add_gadget(&priv_dev->gadget);
3246 dev_err(priv_dev->dev, "Failed to add gadget\n");
3252 kfree(priv_dev->zlp_buf);
3254 dma_free_coherent(priv_dev->sysdev, 8, priv_dev->setup_buf,
3255 priv_dev->setup_dma);
3257 cdns3_free_all_eps(priv_dev);
3259 dma_pool_destroy(priv_dev->eps_dma_pool);
3261 usb_put_gadget(&priv_dev->gadget);
3262 cdns->gadget_dev = NULL;
3266 static int __cdns3_gadget_init(struct cdns *cdns)
3270 /* Ensure 32-bit DMA Mask in case we switched back from Host mode */
3271 ret = dma_set_mask_and_coherent(cdns->dev, DMA_BIT_MASK(32));
3273 dev_err(cdns->dev, "Failed to set dma mask: %d\n", ret);
3277 cdns_drd_gadget_on(cdns);
3278 pm_runtime_get_sync(cdns->dev);
3280 ret = cdns3_gadget_start(cdns);
3282 pm_runtime_put_sync(cdns->dev);
3287 * Because interrupt line can be shared with other components in
3288 * driver it can't use IRQF_ONESHOT flag here.
3290 ret = devm_request_threaded_irq(cdns->dev, cdns->dev_irq,
3291 cdns3_device_irq_handler,
3292 cdns3_device_thread_irq_handler,
3293 IRQF_SHARED, dev_name(cdns->dev),
3301 cdns3_gadget_exit(cdns);
3305 static int cdns3_gadget_suspend(struct cdns *cdns, bool do_wakeup)
3306 __must_hold(&cdns->lock)
3308 struct cdns3_device *priv_dev = cdns->gadget_dev;
3310 spin_unlock(&cdns->lock);
3311 cdns3_disconnect_gadget(priv_dev);
3312 spin_lock(&cdns->lock);
3314 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
3315 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
3316 cdns3_hw_reset_eps_config(priv_dev);
3318 /* disable interrupt for device */
3319 writel(0, &priv_dev->regs->usb_ien);
3324 static int cdns3_gadget_resume(struct cdns *cdns, bool hibernated)
3326 struct cdns3_device *priv_dev = cdns->gadget_dev;
3328 if (!priv_dev->gadget_driver)
3331 cdns3_gadget_config(priv_dev);
3333 writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
3339 * cdns3_gadget_init - initialize device structure
3341 * @cdns: cdns instance
3343 * This function initializes the gadget.
3345 int cdns3_gadget_init(struct cdns *cdns)
3347 struct cdns_role_driver *rdrv;
3349 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
3353 rdrv->start = __cdns3_gadget_init;
3354 rdrv->stop = cdns3_gadget_exit;
3355 rdrv->suspend = cdns3_gadget_suspend;
3356 rdrv->resume = cdns3_gadget_resume;
3357 rdrv->state = CDNS_ROLE_STATE_INACTIVE;
3358 rdrv->name = "gadget";
3359 cdns->roles[USB_ROLE_DEVICE] = rdrv;