1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence CDNSP DRD Driver.
5 * Copyright (C) 2020 Cadence.
9 * Code based on Linux XHCI driver.
10 * Origin: Copyright (C) 2008 Intel Corp
14 * Ring initialization rules:
15 * 1. Each segment is initialized to zero, except for link TRBs.
16 * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
17 * Consumer Cycle State (CCS), depending on ring function.
18 * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
20 * Ring behavior rules:
21 * 1. A ring is empty if enqueue == dequeue. This means there will always be at
22 * least one free TRB in the ring. This is useful if you want to turn that
23 * into a link TRB and expand the ring.
24 * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
25 * link TRB, then load the pointer with the address in the link TRB. If the
26 * link TRB had its toggle bit set, you may need to update the ring cycle
27 * state (see cycle bit rules). You may have to do this multiple times
28 * until you reach a non-link TRB.
29 * 3. A ring is full if enqueue++ (for the definition of increment above)
30 * equals the dequeue pointer.
33 * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
34 * in a link TRB, it must toggle the ring cycle state.
35 * 2. When a producer increments an enqueue pointer and encounters a toggle bit
36 * in a link TRB, it must toggle the ring cycle state.
39 * 1. Check if ring is full before you enqueue.
40 * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
41 * Update enqueue pointer between each write (which may update the ring
43 * 3. Notify consumer. If SW is producer, it rings the doorbell for command
44 * and endpoint rings. If controller is the producer for the event ring,
45 * and it generates an interrupt according to interrupt modulation rules.
48 * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
49 * the TRB is owned by the consumer.
50 * 2. Update dequeue pointer (which may update the ring cycle state) and
51 * continue processing TRBs until you reach a TRB which is not owned by you.
52 * 3. Notify the producer. SW is the consumer for the event ring, and it
53 * updates event ring dequeue pointer. Controller is the consumer for the
54 * command and endpoint rings; it generates events on the event ring
58 #include <linux/scatterlist.h>
59 #include <linux/dma-mapping.h>
60 #include <linux/delay.h>
61 #include <linux/slab.h>
62 #include <linux/irq.h>
64 #include "cdnsp-trace.h"
65 #include "cdnsp-gadget.h"
68 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
71 dma_addr_t cdnsp_trb_virt_to_dma(struct cdnsp_segment *seg,
74 unsigned long segment_offset = trb - seg->trbs;
76 if (trb < seg->trbs || segment_offset >= TRBS_PER_SEGMENT)
79 return seg->dma + (segment_offset * sizeof(*trb));
82 static bool cdnsp_trb_is_noop(union cdnsp_trb *trb)
84 return TRB_TYPE_NOOP_LE32(trb->generic.field[3]);
87 static bool cdnsp_trb_is_link(union cdnsp_trb *trb)
89 return TRB_TYPE_LINK_LE32(trb->link.control);
92 bool cdnsp_last_trb_on_seg(struct cdnsp_segment *seg, union cdnsp_trb *trb)
94 return trb == &seg->trbs[TRBS_PER_SEGMENT - 1];
97 bool cdnsp_last_trb_on_ring(struct cdnsp_ring *ring,
98 struct cdnsp_segment *seg,
101 return cdnsp_last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg);
104 static bool cdnsp_link_trb_toggles_cycle(union cdnsp_trb *trb)
106 return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
109 static void cdnsp_trb_to_noop(union cdnsp_trb *trb, u32 noop_type)
111 if (cdnsp_trb_is_link(trb)) {
112 /* Unchain chained link TRBs. */
113 trb->link.control &= cpu_to_le32(~TRB_CHAIN);
115 trb->generic.field[0] = 0;
116 trb->generic.field[1] = 0;
117 trb->generic.field[2] = 0;
118 /* Preserve only the cycle bit of this TRB. */
119 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
120 trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type));
125 * Updates trb to point to the next TRB in the ring, and updates seg if the next
126 * TRB is in a new segment. This does not skip over link TRBs, and it does not
127 * effect the ring dequeue or enqueue pointers.
129 static void cdnsp_next_trb(struct cdnsp_device *pdev,
130 struct cdnsp_ring *ring,
131 struct cdnsp_segment **seg,
132 union cdnsp_trb **trb)
134 if (cdnsp_trb_is_link(*trb)) {
136 *trb = ((*seg)->trbs);
143 * See Cycle bit rules. SW is the consumer for the event ring only.
144 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
146 void cdnsp_inc_deq(struct cdnsp_device *pdev, struct cdnsp_ring *ring)
148 /* event ring doesn't have link trbs, check for last trb. */
149 if (ring->type == TYPE_EVENT) {
150 if (!cdnsp_last_trb_on_seg(ring->deq_seg, ring->dequeue)) {
155 if (cdnsp_last_trb_on_ring(ring, ring->deq_seg, ring->dequeue))
156 ring->cycle_state ^= 1;
158 ring->deq_seg = ring->deq_seg->next;
159 ring->dequeue = ring->deq_seg->trbs;
163 /* All other rings have link trbs. */
164 if (!cdnsp_trb_is_link(ring->dequeue)) {
166 ring->num_trbs_free++;
168 while (cdnsp_trb_is_link(ring->dequeue)) {
169 ring->deq_seg = ring->deq_seg->next;
170 ring->dequeue = ring->deq_seg->trbs;
173 trace_cdnsp_inc_deq(ring);
177 * See Cycle bit rules. SW is the consumer for the event ring only.
178 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
180 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
181 * chain bit is set), then set the chain bit in all the following link TRBs.
182 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
183 * have their chain bit cleared (so that each Link TRB is a separate TD).
185 * @more_trbs_coming: Will you enqueue more TRBs before ringing the doorbell.
187 static void cdnsp_inc_enq(struct cdnsp_device *pdev,
188 struct cdnsp_ring *ring,
189 bool more_trbs_coming)
191 union cdnsp_trb *next;
194 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
196 /* If this is not event ring, there is one less usable TRB. */
197 if (!cdnsp_trb_is_link(ring->enqueue))
198 ring->num_trbs_free--;
199 next = ++(ring->enqueue);
201 /* Update the dequeue pointer further if that was a link TRB */
202 while (cdnsp_trb_is_link(next)) {
204 * If the caller doesn't plan on enqueuing more TDs before
205 * ringing the doorbell, then we don't want to give the link TRB
206 * to the hardware just yet. We'll give the link TRB back in
207 * cdnsp_prepare_ring() just before we enqueue the TD at the
210 if (!chain && !more_trbs_coming)
213 next->link.control &= cpu_to_le32(~TRB_CHAIN);
214 next->link.control |= cpu_to_le32(chain);
216 /* Give this link TRB to the hardware */
218 next->link.control ^= cpu_to_le32(TRB_CYCLE);
220 /* Toggle the cycle bit after the last ring segment. */
221 if (cdnsp_link_trb_toggles_cycle(next))
222 ring->cycle_state ^= 1;
224 ring->enq_seg = ring->enq_seg->next;
225 ring->enqueue = ring->enq_seg->trbs;
226 next = ring->enqueue;
229 trace_cdnsp_inc_enq(ring);
233 * Check to see if there's room to enqueue num_trbs on the ring and make sure
234 * enqueue pointer will not advance into dequeue segment.
236 static bool cdnsp_room_on_ring(struct cdnsp_device *pdev,
237 struct cdnsp_ring *ring,
238 unsigned int num_trbs)
240 int num_trbs_in_deq_seg;
242 if (ring->num_trbs_free < num_trbs)
245 if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
246 num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
248 if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
256 * Workaround for L1: controller has issue with resuming from L1 after
257 * setting doorbell for endpoint during L1 state. This function forces
258 * resume signal in such case.
260 static void cdnsp_force_l0_go(struct cdnsp_device *pdev)
262 if (pdev->active_port == &pdev->usb2_port && pdev->gadget.lpm_capable)
263 cdnsp_set_link_state(pdev, &pdev->active_port->regs->portsc, XDEV_U0);
266 /* Ring the doorbell after placing a command on the ring. */
267 void cdnsp_ring_cmd_db(struct cdnsp_device *pdev)
269 writel(DB_VALUE_CMD, &pdev->dba->cmd_db);
273 * Ring the doorbell after placing a transfer on the ring.
274 * Returns true if doorbell was set, otherwise false.
276 static bool cdnsp_ring_ep_doorbell(struct cdnsp_device *pdev,
277 struct cdnsp_ep *pep,
278 unsigned int stream_id)
280 __le32 __iomem *reg_addr = &pdev->dba->ep_db;
281 unsigned int ep_state = pep->ep_state;
282 unsigned int db_value;
285 * Don't ring the doorbell for this endpoint if endpoint is halted or
288 if (ep_state & EP_HALTED || !(ep_state & EP_ENABLED))
291 /* For stream capable endpoints driver can ring doorbell only twice. */
292 if (pep->ep_state & EP_HAS_STREAMS) {
293 if (pep->stream_info.drbls_count >= 2)
296 pep->stream_info.drbls_count++;
299 pep->ep_state &= ~EP_STOPPED;
301 if (pep->idx == 0 && pdev->ep0_stage == CDNSP_DATA_STAGE &&
302 !pdev->ep0_expect_in)
303 db_value = DB_VALUE_EP0_OUT(pep->idx, stream_id);
305 db_value = DB_VALUE(pep->idx, stream_id);
307 trace_cdnsp_tr_drbl(pep, stream_id);
309 writel(db_value, reg_addr);
311 cdnsp_force_l0_go(pdev);
313 /* Doorbell was set. */
318 * Get the right ring for the given pep and stream_id.
319 * If the endpoint supports streams, boundary check the USB request's stream ID.
320 * If the endpoint doesn't support streams, return the singular endpoint ring.
322 static struct cdnsp_ring *cdnsp_get_transfer_ring(struct cdnsp_device *pdev,
323 struct cdnsp_ep *pep,
324 unsigned int stream_id)
326 if (!(pep->ep_state & EP_HAS_STREAMS))
329 if (stream_id == 0 || stream_id >= pep->stream_info.num_streams) {
330 dev_err(pdev->dev, "ERR: %s ring doesn't exist for SID: %d.\n",
331 pep->name, stream_id);
335 return pep->stream_info.stream_rings[stream_id];
338 static struct cdnsp_ring *
339 cdnsp_request_to_transfer_ring(struct cdnsp_device *pdev,
340 struct cdnsp_request *preq)
342 return cdnsp_get_transfer_ring(pdev, preq->pep,
343 preq->request.stream_id);
346 /* Ring the doorbell for any rings with pending requests. */
347 void cdnsp_ring_doorbell_for_active_rings(struct cdnsp_device *pdev,
348 struct cdnsp_ep *pep)
350 struct cdnsp_stream_info *stream_info;
351 unsigned int stream_id;
354 if (pep->ep_state & EP_DIS_IN_RROGRESS)
357 /* A ring has pending Request if its TD list is not empty. */
358 if (!(pep->ep_state & EP_HAS_STREAMS) && pep->number) {
359 if (pep->ring && !list_empty(&pep->ring->td_list))
360 cdnsp_ring_ep_doorbell(pdev, pep, 0);
364 stream_info = &pep->stream_info;
366 for (stream_id = 1; stream_id < stream_info->num_streams; stream_id++) {
367 struct cdnsp_td *td, *td_temp;
368 struct cdnsp_ring *ep_ring;
370 if (stream_info->drbls_count >= 2)
373 ep_ring = cdnsp_get_transfer_ring(pdev, pep, stream_id);
377 if (!ep_ring->stream_active || ep_ring->stream_rejected)
380 list_for_each_entry_safe(td, td_temp, &ep_ring->td_list,
385 ret = cdnsp_ring_ep_doorbell(pdev, pep, stream_id);
393 * Get the hw dequeue pointer controller stopped on, either directly from the
394 * endpoint context, or if streams are in use from the stream context.
395 * The returned hw_dequeue contains the lowest four bits with cycle state
396 * and possible stream context type.
398 static u64 cdnsp_get_hw_deq(struct cdnsp_device *pdev,
399 unsigned int ep_index,
400 unsigned int stream_id)
402 struct cdnsp_stream_ctx *st_ctx;
403 struct cdnsp_ep *pep;
405 pep = &pdev->eps[stream_id];
407 if (pep->ep_state & EP_HAS_STREAMS) {
408 st_ctx = &pep->stream_info.stream_ctx_array[stream_id];
409 return le64_to_cpu(st_ctx->stream_ring);
412 return le64_to_cpu(pep->out_ctx->deq);
416 * Move the controller endpoint ring dequeue pointer past cur_td.
417 * Record the new state of the controller endpoint ring dequeue segment,
418 * dequeue pointer, and new consumer cycle state in state.
419 * Update internal representation of the ring's dequeue pointer.
421 * We do this in three jumps:
422 * - First we update our new ring state to be the same as when the
423 * controller stopped.
424 * - Then we traverse the ring to find the segment that contains
425 * the last TRB in the TD. We toggle the controller new cycle state
426 * when we pass any link TRBs with the toggle cycle bit set.
427 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
428 * if we've moved it past a link TRB with the toggle cycle bit set.
430 static void cdnsp_find_new_dequeue_state(struct cdnsp_device *pdev,
431 struct cdnsp_ep *pep,
432 unsigned int stream_id,
433 struct cdnsp_td *cur_td,
434 struct cdnsp_dequeue_state *state)
436 bool td_last_trb_found = false;
437 struct cdnsp_segment *new_seg;
438 struct cdnsp_ring *ep_ring;
439 union cdnsp_trb *new_deq;
440 bool cycle_found = false;
443 ep_ring = cdnsp_get_transfer_ring(pdev, pep, stream_id);
448 * Dig out the cycle state saved by the controller during the
449 * stop endpoint command.
451 hw_dequeue = cdnsp_get_hw_deq(pdev, pep->idx, stream_id);
452 new_seg = ep_ring->deq_seg;
453 new_deq = ep_ring->dequeue;
454 state->new_cycle_state = hw_dequeue & 0x1;
455 state->stream_id = stream_id;
458 * We want to find the pointer, segment and cycle state of the new trb
459 * (the one after current TD's last_trb). We know the cycle state at
460 * hw_dequeue, so walk the ring until both hw_dequeue and last_trb are
464 if (!cycle_found && cdnsp_trb_virt_to_dma(new_seg, new_deq)
465 == (dma_addr_t)(hw_dequeue & ~0xf)) {
468 if (td_last_trb_found)
472 if (new_deq == cur_td->last_trb)
473 td_last_trb_found = true;
475 if (cycle_found && cdnsp_trb_is_link(new_deq) &&
476 cdnsp_link_trb_toggles_cycle(new_deq))
477 state->new_cycle_state ^= 0x1;
479 cdnsp_next_trb(pdev, ep_ring, &new_seg, &new_deq);
481 /* Search wrapped around, bail out. */
482 if (new_deq == pep->ring->dequeue) {
484 "Error: Failed finding new dequeue state\n");
485 state->new_deq_seg = NULL;
486 state->new_deq_ptr = NULL;
490 } while (!cycle_found || !td_last_trb_found);
492 state->new_deq_seg = new_seg;
493 state->new_deq_ptr = new_deq;
495 trace_cdnsp_new_deq_state(state);
499 * flip_cycle means flip the cycle bit of all but the first and last TRB.
500 * (The last TRB actually points to the ring enqueue pointer, which is not part
501 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
503 static void cdnsp_td_to_noop(struct cdnsp_device *pdev,
504 struct cdnsp_ring *ep_ring,
508 struct cdnsp_segment *seg = td->start_seg;
509 union cdnsp_trb *trb = td->first_trb;
512 cdnsp_trb_to_noop(trb, TRB_TR_NOOP);
514 /* flip cycle if asked to */
515 if (flip_cycle && trb != td->first_trb && trb != td->last_trb)
516 trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE);
518 if (trb == td->last_trb)
521 cdnsp_next_trb(pdev, ep_ring, &seg, &trb);
526 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
527 * at end_trb, which may be in another segment. If the suspect DMA address is a
528 * TRB in this TD, this function returns that TRB's segment. Otherwise it
531 static struct cdnsp_segment *cdnsp_trb_in_td(struct cdnsp_device *pdev,
532 struct cdnsp_segment *start_seg,
533 union cdnsp_trb *start_trb,
534 union cdnsp_trb *end_trb,
535 dma_addr_t suspect_dma)
537 struct cdnsp_segment *cur_seg;
538 union cdnsp_trb *temp_trb;
539 dma_addr_t end_seg_dma;
540 dma_addr_t end_trb_dma;
541 dma_addr_t start_dma;
543 start_dma = cdnsp_trb_virt_to_dma(start_seg, start_trb);
550 temp_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1];
551 /* We may get an event for a Link TRB in the middle of a TD */
552 end_seg_dma = cdnsp_trb_virt_to_dma(cur_seg, temp_trb);
553 /* If the end TRB isn't in this segment, this is set to 0 */
554 end_trb_dma = cdnsp_trb_virt_to_dma(cur_seg, end_trb);
556 trace_cdnsp_looking_trb_in_td(suspect_dma, start_dma,
557 end_trb_dma, cur_seg->dma,
560 if (end_trb_dma > 0) {
562 * The end TRB is in this segment, so suspect should
565 if (start_dma <= end_trb_dma) {
566 if (suspect_dma >= start_dma &&
567 suspect_dma <= end_trb_dma) {
572 * Case for one segment with a
573 * TD wrapped around to the top
575 if ((suspect_dma >= start_dma &&
576 suspect_dma <= end_seg_dma) ||
577 (suspect_dma >= cur_seg->dma &&
578 suspect_dma <= end_trb_dma)) {
586 /* Might still be somewhere in this segment */
587 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
590 cur_seg = cur_seg->next;
591 start_dma = cdnsp_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
592 } while (cur_seg != start_seg);
597 static void cdnsp_unmap_td_bounce_buffer(struct cdnsp_device *pdev,
598 struct cdnsp_ring *ring,
601 struct cdnsp_segment *seg = td->bounce_seg;
602 struct cdnsp_request *preq;
610 trace_cdnsp_bounce_unmap(td->preq, seg->bounce_len, seg->bounce_offs,
613 if (!preq->direction) {
614 dma_unmap_single(pdev->dev, seg->bounce_dma,
615 ring->bounce_buf_len, DMA_TO_DEVICE);
619 dma_unmap_single(pdev->dev, seg->bounce_dma, ring->bounce_buf_len,
622 /* For in transfers we need to copy the data from bounce to sg */
623 len = sg_pcopy_from_buffer(preq->request.sg, preq->request.num_sgs,
624 seg->bounce_buf, seg->bounce_len,
626 if (len != seg->bounce_len)
627 dev_warn(pdev->dev, "WARN Wrong bounce buffer read length: %zu != %d\n",
628 len, seg->bounce_len);
631 seg->bounce_offs = 0;
634 static int cdnsp_cmd_set_deq(struct cdnsp_device *pdev,
635 struct cdnsp_ep *pep,
636 struct cdnsp_dequeue_state *deq_state)
638 struct cdnsp_ring *ep_ring;
641 if (!deq_state->new_deq_ptr || !deq_state->new_deq_seg) {
642 cdnsp_ring_doorbell_for_active_rings(pdev, pep);
646 cdnsp_queue_new_dequeue_state(pdev, pep, deq_state);
647 cdnsp_ring_cmd_db(pdev);
648 ret = cdnsp_wait_for_cmd_compl(pdev);
650 trace_cdnsp_handle_cmd_set_deq(cdnsp_get_slot_ctx(&pdev->out_ctx));
651 trace_cdnsp_handle_cmd_set_deq_ep(pep->out_ctx);
654 * Update the ring's dequeue segment and dequeue pointer
655 * to reflect the new position.
657 ep_ring = cdnsp_get_transfer_ring(pdev, pep, deq_state->stream_id);
659 if (cdnsp_trb_is_link(ep_ring->dequeue)) {
660 ep_ring->deq_seg = ep_ring->deq_seg->next;
661 ep_ring->dequeue = ep_ring->deq_seg->trbs;
664 while (ep_ring->dequeue != deq_state->new_deq_ptr) {
665 ep_ring->num_trbs_free++;
668 if (cdnsp_trb_is_link(ep_ring->dequeue)) {
669 if (ep_ring->dequeue == deq_state->new_deq_ptr)
672 ep_ring->deq_seg = ep_ring->deq_seg->next;
673 ep_ring->dequeue = ep_ring->deq_seg->trbs;
678 * Probably there was TIMEOUT during handling Set Dequeue Pointer
679 * command. It's critical error and controller will be stopped.
684 /* Restart any rings with pending requests */
685 cdnsp_ring_doorbell_for_active_rings(pdev, pep);
690 int cdnsp_remove_request(struct cdnsp_device *pdev,
691 struct cdnsp_request *preq,
692 struct cdnsp_ep *pep)
694 struct cdnsp_dequeue_state deq_state;
695 struct cdnsp_td *cur_td = NULL;
696 struct cdnsp_ring *ep_ring;
697 struct cdnsp_segment *seg;
698 int status = -ECONNRESET;
702 memset(&deq_state, 0, sizeof(deq_state));
704 trace_cdnsp_remove_request(pep->out_ctx);
705 trace_cdnsp_remove_request_td(preq);
708 ep_ring = cdnsp_request_to_transfer_ring(pdev, preq);
711 * If we stopped on the TD we need to cancel, then we have to
712 * move the controller endpoint ring dequeue pointer past
715 hw_deq = cdnsp_get_hw_deq(pdev, pep->idx, preq->request.stream_id);
718 seg = cdnsp_trb_in_td(pdev, cur_td->start_seg, cur_td->first_trb,
719 cur_td->last_trb, hw_deq);
721 if (seg && (pep->ep_state & EP_ENABLED))
722 cdnsp_find_new_dequeue_state(pdev, pep, preq->request.stream_id,
725 cdnsp_td_to_noop(pdev, ep_ring, cur_td, false);
728 * The event handler won't see a completion for this TD anymore,
729 * so remove it from the endpoint ring's TD list.
731 list_del_init(&cur_td->td_list);
733 pep->stream_info.td_count--;
736 * During disconnecting all endpoint will be disabled so we don't
737 * have to worry about updating dequeue pointer.
739 if (pdev->cdnsp_state & CDNSP_STATE_DISCONNECT_PENDING) {
741 ret = cdnsp_cmd_set_deq(pdev, pep, &deq_state);
744 cdnsp_unmap_td_bounce_buffer(pdev, ep_ring, cur_td);
745 cdnsp_gadget_giveback(pep, cur_td->preq, status);
750 static int cdnsp_update_port_id(struct cdnsp_device *pdev, u32 port_id)
752 struct cdnsp_port *port = pdev->active_port;
755 if (port && port->port_num == port_id)
759 old_port = port->port_num;
761 if (port_id == pdev->usb2_port.port_num) {
762 port = &pdev->usb2_port;
763 } else if (port_id == pdev->usb3_port.port_num) {
764 port = &pdev->usb3_port;
766 dev_err(pdev->dev, "Port event with invalid port ID %d\n",
771 if (port_id != old_port) {
772 cdnsp_disable_slot(pdev);
773 pdev->active_port = port;
774 cdnsp_enable_slot(pdev);
777 if (port_id == pdev->usb2_port.port_num)
778 cdnsp_set_usb2_hardware_lpm(pdev, NULL, 1);
780 writel(PORT_U1_TIMEOUT(1) | PORT_U2_TIMEOUT(1),
781 &pdev->usb3_port.regs->portpmsc);
786 static void cdnsp_handle_port_status(struct cdnsp_device *pdev,
787 union cdnsp_trb *event)
789 struct cdnsp_port_regs __iomem *port_regs;
790 u32 portsc, cmd_regs;
795 /* Port status change events always have a successful completion code */
796 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS)
797 dev_err(pdev->dev, "ERR: incorrect PSC event\n");
799 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
801 if (cdnsp_update_port_id(pdev, port_id))
804 port_regs = pdev->active_port->regs;
806 if (port_id == pdev->usb2_port.port_num)
810 portsc = readl(&port_regs->portsc);
811 writel(cdnsp_port_state_to_neutral(portsc) |
812 (portsc & PORT_CHANGE_BITS), &port_regs->portsc);
814 trace_cdnsp_handle_port_status(pdev->active_port->port_num, portsc);
816 pdev->gadget.speed = cdnsp_port_speed(portsc);
817 link_state = portsc & PORT_PLS_MASK;
819 /* Port Link State change detected. */
820 if ((portsc & PORT_PLC)) {
821 if (!(pdev->cdnsp_state & CDNSP_WAKEUP_PENDING) &&
822 link_state == XDEV_RESUME) {
823 cmd_regs = readl(&pdev->op_regs->command);
824 if (!(cmd_regs & CMD_R_S))
827 if (DEV_SUPERSPEED_ANY(portsc)) {
828 cdnsp_set_link_state(pdev, &port_regs->portsc,
831 cdnsp_resume_gadget(pdev);
835 if ((pdev->cdnsp_state & CDNSP_WAKEUP_PENDING) &&
836 link_state == XDEV_U0) {
837 pdev->cdnsp_state &= ~CDNSP_WAKEUP_PENDING;
839 cdnsp_force_header_wakeup(pdev, 1);
840 cdnsp_ring_cmd_db(pdev);
841 cdnsp_wait_for_cmd_compl(pdev);
844 if (link_state == XDEV_U0 && pdev->link_state == XDEV_U3 &&
845 !DEV_SUPERSPEED_ANY(portsc))
846 cdnsp_resume_gadget(pdev);
848 if (link_state == XDEV_U3 && pdev->link_state != XDEV_U3)
849 cdnsp_suspend_gadget(pdev);
851 pdev->link_state = link_state;
854 if (portsc & PORT_CSC) {
856 if (pdev->gadget.connected && !(portsc & PORT_CONNECT))
857 cdnsp_disconnect_gadget(pdev);
860 if (portsc & PORT_CONNECT) {
862 cdnsp_irq_reset(pdev);
864 usb_gadget_set_state(&pdev->gadget, USB_STATE_ATTACHED);
869 if ((portsc & (PORT_RC | PORT_WRC)) && (portsc & PORT_CONNECT)) {
870 cdnsp_irq_reset(pdev);
871 pdev->u1_allowed = 0;
872 pdev->u2_allowed = 0;
873 pdev->may_wakeup = 0;
876 if (portsc & PORT_CEC)
877 dev_err(pdev->dev, "Port Over Current detected\n");
879 if (portsc & PORT_CEC)
880 dev_err(pdev->dev, "Port Configure Error detected\n");
882 if (readl(&port_regs->portsc) & PORT_CHANGE_BITS)
886 cdnsp_inc_deq(pdev, pdev->event_ring);
889 static void cdnsp_td_cleanup(struct cdnsp_device *pdev,
891 struct cdnsp_ring *ep_ring,
894 struct cdnsp_request *preq = td->preq;
896 /* if a bounce buffer was used to align this td then unmap it */
897 cdnsp_unmap_td_bounce_buffer(pdev, ep_ring, td);
900 * If the controller said we transferred more data than the buffer
901 * length, Play it safe and say we didn't transfer anything.
903 if (preq->request.actual > preq->request.length) {
904 preq->request.actual = 0;
908 list_del_init(&td->td_list);
910 preq->pep->stream_info.td_count--;
912 cdnsp_gadget_giveback(preq->pep, preq, *status);
915 static void cdnsp_finish_td(struct cdnsp_device *pdev,
917 struct cdnsp_transfer_event *event,
921 struct cdnsp_ring *ep_ring;
924 ep_ring = cdnsp_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
925 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
927 if (trb_comp_code == COMP_STOPPED_LENGTH_INVALID ||
928 trb_comp_code == COMP_STOPPED ||
929 trb_comp_code == COMP_STOPPED_SHORT_PACKET) {
931 * The Endpoint Stop Command completion will take care of any
932 * stopped TDs. A stopped TD may be restarted, so don't update
933 * the ring dequeue pointer or take this TD off any lists yet.
938 /* Update ring dequeue pointer */
939 while (ep_ring->dequeue != td->last_trb)
940 cdnsp_inc_deq(pdev, ep_ring);
942 cdnsp_inc_deq(pdev, ep_ring);
944 cdnsp_td_cleanup(pdev, td, ep_ring, status);
947 /* sum trb lengths from ring dequeue up to stop_trb, _excluding_ stop_trb */
948 static int cdnsp_sum_trb_lengths(struct cdnsp_device *pdev,
949 struct cdnsp_ring *ring,
950 union cdnsp_trb *stop_trb)
952 struct cdnsp_segment *seg = ring->deq_seg;
953 union cdnsp_trb *trb = ring->dequeue;
956 for (sum = 0; trb != stop_trb; cdnsp_next_trb(pdev, ring, &seg, &trb)) {
957 if (!cdnsp_trb_is_noop(trb) && !cdnsp_trb_is_link(trb))
958 sum += TRB_LEN(le32_to_cpu(trb->generic.field[2]));
963 static int cdnsp_giveback_first_trb(struct cdnsp_device *pdev,
964 struct cdnsp_ep *pep,
965 unsigned int stream_id,
967 struct cdnsp_generic_trb *start_trb)
970 * Pass all the TRBs to the hardware at once and make sure this write
976 start_trb->field[3] |= cpu_to_le32(start_cycle);
978 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
980 if ((pep->ep_state & EP_HAS_STREAMS) &&
981 !pep->stream_info.first_prime_det) {
982 trace_cdnsp_wait_for_prime(pep, stream_id);
986 return cdnsp_ring_ep_doorbell(pdev, pep, stream_id);
990 * Process control tds, update USB request status and actual_length.
992 static void cdnsp_process_ctrl_td(struct cdnsp_device *pdev,
994 union cdnsp_trb *event_trb,
995 struct cdnsp_transfer_event *event,
996 struct cdnsp_ep *pep,
999 struct cdnsp_ring *ep_ring;
1003 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event_trb->generic.field[3]));
1004 ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer));
1005 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
1008 * if on data stage then update the actual_length of the USB
1009 * request and flag it as set, so it won't be overwritten in the event
1012 if (trb_type == TRB_DATA) {
1013 td->request_length_set = true;
1014 td->preq->request.actual = td->preq->request.length - remaining;
1017 /* at status stage */
1018 if (!td->request_length_set)
1019 td->preq->request.actual = td->preq->request.length;
1021 if (pdev->ep0_stage == CDNSP_DATA_STAGE && pep->number == 0 &&
1022 pdev->three_stage_setup) {
1023 td = list_entry(ep_ring->td_list.next, struct cdnsp_td,
1025 pdev->ep0_stage = CDNSP_STATUS_STAGE;
1027 cdnsp_giveback_first_trb(pdev, pep, 0, ep_ring->cycle_state,
1028 &td->last_trb->generic);
1032 cdnsp_finish_td(pdev, td, event, pep, status);
1036 * Process isochronous tds, update usb request status and actual_length.
1038 static void cdnsp_process_isoc_td(struct cdnsp_device *pdev,
1039 struct cdnsp_td *td,
1040 union cdnsp_trb *ep_trb,
1041 struct cdnsp_transfer_event *event,
1042 struct cdnsp_ep *pep,
1045 struct cdnsp_request *preq = td->preq;
1046 u32 remaining, requested, ep_trb_len;
1047 bool sum_trbs_for_length = false;
1048 struct cdnsp_ring *ep_ring;
1052 ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer));
1053 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
1054 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
1055 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
1057 requested = preq->request.length;
1059 /* handle completion code */
1060 switch (trb_comp_code) {
1062 preq->request.status = 0;
1064 case COMP_SHORT_PACKET:
1065 preq->request.status = 0;
1066 sum_trbs_for_length = true;
1068 case COMP_ISOCH_BUFFER_OVERRUN:
1069 case COMP_BABBLE_DETECTED_ERROR:
1070 preq->request.status = -EOVERFLOW;
1073 sum_trbs_for_length = true;
1075 case COMP_STOPPED_SHORT_PACKET:
1076 /* field normally containing residue now contains transferred */
1077 preq->request.status = 0;
1078 requested = remaining;
1080 case COMP_STOPPED_LENGTH_INVALID:
1085 sum_trbs_for_length = true;
1086 preq->request.status = -1;
1090 if (sum_trbs_for_length) {
1091 td_length = cdnsp_sum_trb_lengths(pdev, ep_ring, ep_trb);
1092 td_length += ep_trb_len - remaining;
1094 td_length = requested;
1097 td->preq->request.actual += td_length;
1099 cdnsp_finish_td(pdev, td, event, pep, &status);
1102 static void cdnsp_skip_isoc_td(struct cdnsp_device *pdev,
1103 struct cdnsp_td *td,
1104 struct cdnsp_transfer_event *event,
1105 struct cdnsp_ep *pep,
1108 struct cdnsp_ring *ep_ring;
1110 ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer));
1111 td->preq->request.status = -EXDEV;
1112 td->preq->request.actual = 0;
1114 /* Update ring dequeue pointer */
1115 while (ep_ring->dequeue != td->last_trb)
1116 cdnsp_inc_deq(pdev, ep_ring);
1118 cdnsp_inc_deq(pdev, ep_ring);
1120 cdnsp_td_cleanup(pdev, td, ep_ring, &status);
1124 * Process bulk and interrupt tds, update usb request status and actual_length.
1126 static void cdnsp_process_bulk_intr_td(struct cdnsp_device *pdev,
1127 struct cdnsp_td *td,
1128 union cdnsp_trb *ep_trb,
1129 struct cdnsp_transfer_event *event,
1130 struct cdnsp_ep *ep,
1133 u32 remaining, requested, ep_trb_len;
1134 struct cdnsp_ring *ep_ring;
1137 ep_ring = cdnsp_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
1138 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
1139 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
1140 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
1141 requested = td->preq->request.length;
1143 switch (trb_comp_code) {
1145 case COMP_SHORT_PACKET:
1148 case COMP_STOPPED_SHORT_PACKET:
1149 td->preq->request.actual = remaining;
1151 case COMP_STOPPED_LENGTH_INVALID:
1152 /* Stopped on ep trb with invalid length, exclude it. */
1158 if (ep_trb == td->last_trb)
1159 ep_trb_len = requested - remaining;
1161 ep_trb_len = cdnsp_sum_trb_lengths(pdev, ep_ring, ep_trb) +
1162 ep_trb_len - remaining;
1163 td->preq->request.actual = ep_trb_len;
1166 ep->stream_info.drbls_count--;
1168 cdnsp_finish_td(pdev, td, event, ep, status);
1171 static void cdnsp_handle_tx_nrdy(struct cdnsp_device *pdev,
1172 struct cdnsp_transfer_event *event)
1174 struct cdnsp_generic_trb *generic;
1175 struct cdnsp_ring *ep_ring;
1176 struct cdnsp_ep *pep;
1182 generic = (struct cdnsp_generic_trb *)event;
1183 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1184 dev_sid = TRB_TO_DEV_STREAM(le32_to_cpu(generic->field[0]));
1185 host_sid = TRB_TO_HOST_STREAM(le32_to_cpu(generic->field[2]));
1187 pep = &pdev->eps[ep_index];
1189 if (!(pep->ep_state & EP_HAS_STREAMS))
1192 if (host_sid == STREAM_PRIME_ACK) {
1193 pep->stream_info.first_prime_det = 1;
1194 for (cur_stream = 1; cur_stream < pep->stream_info.num_streams;
1196 ep_ring = pep->stream_info.stream_rings[cur_stream];
1197 ep_ring->stream_active = 1;
1198 ep_ring->stream_rejected = 0;
1202 if (host_sid == STREAM_REJECTED) {
1203 struct cdnsp_td *td, *td_temp;
1205 pep->stream_info.drbls_count--;
1206 ep_ring = pep->stream_info.stream_rings[dev_sid];
1207 ep_ring->stream_active = 0;
1208 ep_ring->stream_rejected = 1;
1210 list_for_each_entry_safe(td, td_temp, &ep_ring->td_list,
1216 cdnsp_ring_doorbell_for_active_rings(pdev, pep);
1220 * If this function returns an error condition, it means it got a Transfer
1221 * event with a corrupted TRB DMA address or endpoint is disabled.
1223 static int cdnsp_handle_tx_event(struct cdnsp_device *pdev,
1224 struct cdnsp_transfer_event *event)
1226 const struct usb_endpoint_descriptor *desc;
1227 bool handling_skipped_tds = false;
1228 struct cdnsp_segment *ep_seg;
1229 struct cdnsp_ring *ep_ring;
1230 int status = -EINPROGRESS;
1231 union cdnsp_trb *ep_trb;
1232 dma_addr_t ep_trb_dma;
1233 struct cdnsp_ep *pep;
1234 struct cdnsp_td *td;
1239 invalidate = le32_to_cpu(event->flags) & TRB_EVENT_INVALIDATE;
1240 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
1241 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
1242 ep_trb_dma = le64_to_cpu(event->buffer);
1244 pep = &pdev->eps[ep_index];
1245 ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer));
1248 * If device is disconnect then all requests will be dequeued
1249 * by upper layers as part of disconnect sequence.
1250 * We don't want handle such event to avoid racing.
1252 if (invalidate || !pdev->gadget.connected)
1255 if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_DISABLED) {
1256 trace_cdnsp_ep_disabled(pep->out_ctx);
1260 /* Some transfer events don't always point to a trb*/
1262 switch (trb_comp_code) {
1263 case COMP_INVALID_STREAM_TYPE_ERROR:
1264 case COMP_INVALID_STREAM_ID_ERROR:
1265 case COMP_RING_UNDERRUN:
1266 case COMP_RING_OVERRUN:
1269 dev_err(pdev->dev, "ERROR: %s event for unknown ring\n",
1275 /* Look for some error cases that need special treatment. */
1276 switch (trb_comp_code) {
1277 case COMP_BABBLE_DETECTED_ERROR:
1278 status = -EOVERFLOW;
1280 case COMP_RING_UNDERRUN:
1281 case COMP_RING_OVERRUN:
1283 * When the Isoch ring is empty, the controller will generate
1284 * a Ring Overrun Event for IN Isoch endpoint or Ring
1285 * Underrun Event for OUT Isoch endpoint.
1288 case COMP_MISSED_SERVICE_ERROR:
1290 * When encounter missed service error, one or more isoc tds
1291 * may be missed by controller.
1292 * Set skip flag of the ep_ring; Complete the missed tds as
1293 * short transfer when process the ep_ring next time.
1301 * This TRB should be in the TD at the head of this ring's TD
1304 if (list_empty(&ep_ring->td_list)) {
1306 * Don't print warnings if it's due to a stopped
1307 * endpoint generating an extra completion event, or
1308 * a event for the last TRB of a short TD we already
1309 * got a short event for.
1310 * The short TD is already removed from the TD list.
1312 if (!(trb_comp_code == COMP_STOPPED ||
1313 trb_comp_code == COMP_STOPPED_LENGTH_INVALID ||
1314 ep_ring->last_td_was_short))
1315 trace_cdnsp_trb_without_td(ep_ring,
1316 (struct cdnsp_generic_trb *)event);
1320 trace_cdnsp_ep_list_empty_with_skip(pep, 0);
1326 td = list_entry(ep_ring->td_list.next, struct cdnsp_td,
1329 /* Is this a TRB in the currently executing TD? */
1330 ep_seg = cdnsp_trb_in_td(pdev, ep_ring->deq_seg,
1331 ep_ring->dequeue, td->last_trb,
1335 * Skip the Force Stopped Event. The event_trb(ep_trb_dma)
1336 * of FSE is not in the current TD pointed by ep_ring->dequeue
1337 * because that the hardware dequeue pointer still at the
1338 * previous TRB of the current TD. The previous TRB maybe a
1339 * Link TD or the last TRB of the previous TD. The command
1340 * completion handle will take care the rest.
1342 if (!ep_seg && (trb_comp_code == COMP_STOPPED ||
1343 trb_comp_code == COMP_STOPPED_LENGTH_INVALID)) {
1348 desc = td->preq->pep->endpoint.desc;
1350 if (!pep->skip || !usb_endpoint_xfer_isoc(desc)) {
1351 /* Something is busted, give up! */
1353 "ERROR Transfer event TRB DMA ptr not "
1354 "part of current TD ep_index %d "
1355 "comp_code %u\n", ep_index,
1360 cdnsp_skip_isoc_td(pdev, td, event, pep, status);
1364 if (trb_comp_code == COMP_SHORT_PACKET)
1365 ep_ring->last_td_was_short = true;
1367 ep_ring->last_td_was_short = false;
1371 cdnsp_skip_isoc_td(pdev, td, event, pep, status);
1375 ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma)
1378 trace_cdnsp_handle_transfer(ep_ring,
1379 (struct cdnsp_generic_trb *)ep_trb);
1381 if (cdnsp_trb_is_noop(ep_trb))
1384 if (usb_endpoint_xfer_control(desc))
1385 cdnsp_process_ctrl_td(pdev, td, ep_trb, event, pep,
1387 else if (usb_endpoint_xfer_isoc(desc))
1388 cdnsp_process_isoc_td(pdev, td, ep_trb, event, pep,
1391 cdnsp_process_bulk_intr_td(pdev, td, ep_trb, event, pep,
1394 handling_skipped_tds = pep->skip;
1397 * Do not update event ring dequeue pointer if we're in a loop
1398 * processing missed tds.
1400 if (!handling_skipped_tds)
1401 cdnsp_inc_deq(pdev, pdev->event_ring);
1404 * If ep->skip is set, it means there are missed tds on the
1405 * endpoint ring need to take care of.
1406 * Process them as short transfer until reach the td pointed by
1409 } while (handling_skipped_tds);
1413 dev_err(pdev->dev, "@%016llx %08x %08x %08x %08x\n",
1414 (unsigned long long)
1415 cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
1416 pdev->event_ring->dequeue),
1417 lower_32_bits(le64_to_cpu(event->buffer)),
1418 upper_32_bits(le64_to_cpu(event->buffer)),
1419 le32_to_cpu(event->transfer_len),
1420 le32_to_cpu(event->flags));
1425 * This function handles all events on the event ring.
1426 * Returns true for "possibly more events to process" (caller should call
1427 * again), otherwise false if done.
1429 static bool cdnsp_handle_event(struct cdnsp_device *pdev)
1431 unsigned int comp_code;
1432 union cdnsp_trb *event;
1433 bool update_ptrs = true;
1438 event = pdev->event_ring->dequeue;
1439 flags = le32_to_cpu(event->event_cmd.flags);
1440 cycle_bit = (flags & TRB_CYCLE);
1442 /* Does the controller or driver own the TRB? */
1443 if (cycle_bit != pdev->event_ring->cycle_state)
1446 trace_cdnsp_handle_event(pdev->event_ring, &event->generic);
1449 * Barrier between reading the TRB_CYCLE (valid) flag above and any
1450 * reads of the event's flags/data below.
1454 switch (flags & TRB_TYPE_BITMASK) {
1455 case TRB_TYPE(TRB_COMPLETION):
1457 * Command can't be handled in interrupt context so just
1458 * increment command ring dequeue pointer.
1460 cdnsp_inc_deq(pdev, pdev->cmd_ring);
1462 case TRB_TYPE(TRB_PORT_STATUS):
1463 cdnsp_handle_port_status(pdev, event);
1464 update_ptrs = false;
1466 case TRB_TYPE(TRB_TRANSFER):
1467 ret = cdnsp_handle_tx_event(pdev, &event->trans_event);
1469 update_ptrs = false;
1471 case TRB_TYPE(TRB_SETUP):
1472 pdev->ep0_stage = CDNSP_SETUP_STAGE;
1473 pdev->setup_id = TRB_SETUPID_TO_TYPE(flags);
1474 pdev->setup_speed = TRB_SETUP_SPEEDID(flags);
1475 pdev->setup = *((struct usb_ctrlrequest *)
1476 &event->trans_event.buffer);
1478 cdnsp_setup_analyze(pdev);
1480 case TRB_TYPE(TRB_ENDPOINT_NRDY):
1481 cdnsp_handle_tx_nrdy(pdev, &event->trans_event);
1483 case TRB_TYPE(TRB_HC_EVENT): {
1484 comp_code = GET_COMP_CODE(le32_to_cpu(event->generic.field[2]));
1486 switch (comp_code) {
1487 case COMP_EVENT_RING_FULL_ERROR:
1488 dev_err(pdev->dev, "Event Ring Full\n");
1491 dev_err(pdev->dev, "Controller error code 0x%02x\n",
1497 case TRB_TYPE(TRB_MFINDEX_WRAP):
1498 case TRB_TYPE(TRB_DRB_OVERFLOW):
1501 dev_warn(pdev->dev, "ERROR unknown event type %ld\n",
1502 TRB_FIELD_TO_TYPE(flags));
1506 /* Update SW event ring dequeue pointer. */
1507 cdnsp_inc_deq(pdev, pdev->event_ring);
1510 * Caller will call us again to check if there are more items
1511 * on the event ring.
1516 irqreturn_t cdnsp_thread_irq_handler(int irq, void *data)
1518 struct cdnsp_device *pdev = (struct cdnsp_device *)data;
1519 union cdnsp_trb *event_ring_deq;
1522 spin_lock(&pdev->lock);
1524 if (pdev->cdnsp_state & (CDNSP_STATE_HALTED | CDNSP_STATE_DYING)) {
1526 spin_unlock(&pdev->lock);
1530 event_ring_deq = pdev->event_ring->dequeue;
1532 while (cdnsp_handle_event(pdev)) {
1533 if (++counter >= TRBS_PER_EV_DEQ_UPDATE) {
1534 cdnsp_update_erst_dequeue(pdev, event_ring_deq, 0);
1535 event_ring_deq = pdev->event_ring->dequeue;
1540 cdnsp_update_erst_dequeue(pdev, event_ring_deq, 1);
1542 spin_unlock(&pdev->lock);
1547 irqreturn_t cdnsp_irq_handler(int irq, void *priv)
1549 struct cdnsp_device *pdev = (struct cdnsp_device *)priv;
1553 status = readl(&pdev->op_regs->status);
1555 if (status == ~(u32)0) {
1560 if (!(status & STS_EINT))
1563 writel(status | STS_EINT, &pdev->op_regs->status);
1564 irq_pending = readl(&pdev->ir_set->irq_pending);
1565 irq_pending |= IMAN_IP;
1566 writel(irq_pending, &pdev->ir_set->irq_pending);
1568 if (status & STS_FATAL) {
1573 return IRQ_WAKE_THREAD;
1577 * Generic function for queuing a TRB on a ring.
1578 * The caller must have checked to make sure there's room on the ring.
1580 * @more_trbs_coming: Will you enqueue more TRBs before setting doorbell?
1582 static void cdnsp_queue_trb(struct cdnsp_device *pdev, struct cdnsp_ring *ring,
1583 bool more_trbs_coming, u32 field1, u32 field2,
1584 u32 field3, u32 field4)
1586 struct cdnsp_generic_trb *trb;
1588 trb = &ring->enqueue->generic;
1590 trb->field[0] = cpu_to_le32(field1);
1591 trb->field[1] = cpu_to_le32(field2);
1592 trb->field[2] = cpu_to_le32(field3);
1593 trb->field[3] = cpu_to_le32(field4);
1595 trace_cdnsp_queue_trb(ring, trb);
1596 cdnsp_inc_enq(pdev, ring, more_trbs_coming);
1600 * Does various checks on the endpoint ring, and makes it ready to
1603 static int cdnsp_prepare_ring(struct cdnsp_device *pdev,
1604 struct cdnsp_ring *ep_ring,
1605 u32 ep_state, unsigned
1609 unsigned int num_trbs_needed;
1611 /* Make sure the endpoint has been added to controller schedule. */
1613 case EP_STATE_STOPPED:
1614 case EP_STATE_RUNNING:
1615 case EP_STATE_HALTED:
1618 dev_err(pdev->dev, "ERROR: incorrect endpoint state\n");
1623 if (cdnsp_room_on_ring(pdev, ep_ring, num_trbs))
1626 trace_cdnsp_no_room_on_ring("try ring expansion");
1628 num_trbs_needed = num_trbs - ep_ring->num_trbs_free;
1629 if (cdnsp_ring_expansion(pdev, ep_ring, num_trbs_needed,
1631 dev_err(pdev->dev, "Ring expansion failed\n");
1636 while (cdnsp_trb_is_link(ep_ring->enqueue)) {
1637 ep_ring->enqueue->link.control |= cpu_to_le32(TRB_CHAIN);
1638 /* The cycle bit must be set as the last operation. */
1640 ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE);
1642 /* Toggle the cycle bit after the last ring segment. */
1643 if (cdnsp_link_trb_toggles_cycle(ep_ring->enqueue))
1644 ep_ring->cycle_state ^= 1;
1645 ep_ring->enq_seg = ep_ring->enq_seg->next;
1646 ep_ring->enqueue = ep_ring->enq_seg->trbs;
1651 static int cdnsp_prepare_transfer(struct cdnsp_device *pdev,
1652 struct cdnsp_request *preq,
1653 unsigned int num_trbs)
1655 struct cdnsp_ring *ep_ring;
1658 ep_ring = cdnsp_get_transfer_ring(pdev, preq->pep,
1659 preq->request.stream_id);
1663 ret = cdnsp_prepare_ring(pdev, ep_ring,
1664 GET_EP_CTX_STATE(preq->pep->out_ctx),
1665 num_trbs, GFP_ATOMIC);
1669 INIT_LIST_HEAD(&preq->td.td_list);
1670 preq->td.preq = preq;
1672 /* Add this TD to the tail of the endpoint ring's TD list. */
1673 list_add_tail(&preq->td.td_list, &ep_ring->td_list);
1675 preq->pep->stream_info.td_count++;
1677 preq->td.start_seg = ep_ring->enq_seg;
1678 preq->td.first_trb = ep_ring->enqueue;
1683 static unsigned int cdnsp_count_trbs(u64 addr, u64 len)
1685 unsigned int num_trbs;
1687 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
1695 static unsigned int count_trbs_needed(struct cdnsp_request *preq)
1697 return cdnsp_count_trbs(preq->request.dma, preq->request.length);
1700 static unsigned int count_sg_trbs_needed(struct cdnsp_request *preq)
1702 unsigned int i, len, full_len, num_trbs = 0;
1703 struct scatterlist *sg;
1705 full_len = preq->request.length;
1707 for_each_sg(preq->request.sg, sg, preq->request.num_sgs, i) {
1708 len = sg_dma_len(sg);
1709 num_trbs += cdnsp_count_trbs(sg_dma_address(sg), len);
1710 len = min(len, full_len);
1719 static unsigned int count_isoc_trbs_needed(struct cdnsp_request *preq)
1721 return cdnsp_count_trbs(preq->request.dma, preq->request.length);
1724 static void cdnsp_check_trb_math(struct cdnsp_request *preq, int running_total)
1726 if (running_total != preq->request.length)
1727 dev_err(preq->pep->pdev->dev,
1728 "%s - Miscalculated tx length, "
1729 "queued %#x, asked for %#x (%d)\n",
1730 preq->pep->name, running_total,
1731 preq->request.length, preq->request.actual);
1735 * TD size is the number of max packet sized packets remaining in the TD
1736 * (*not* including this TRB).
1738 * Total TD packet count = total_packet_count =
1739 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
1741 * Packets transferred up to and including this TRB = packets_transferred =
1742 * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
1744 * TD size = total_packet_count - packets_transferred
1746 * It must fit in bits 21:17, so it can't be bigger than 31.
1747 * This is taken care of in the TRB_TD_SIZE() macro
1749 * The last TRB in a TD must have the TD size set to zero.
1751 static u32 cdnsp_td_remainder(struct cdnsp_device *pdev,
1754 unsigned int td_total_len,
1755 struct cdnsp_request *preq,
1756 bool more_trbs_coming)
1758 u32 maxp, total_packet_count;
1760 /* One TRB with a zero-length data packet. */
1761 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) ||
1762 trb_buff_len == td_total_len)
1765 maxp = usb_endpoint_maxp(preq->pep->endpoint.desc);
1766 total_packet_count = DIV_ROUND_UP(td_total_len, maxp);
1768 /* Queuing functions don't count the current TRB into transferred. */
1769 return (total_packet_count - ((transferred + trb_buff_len) / maxp));
1772 static int cdnsp_align_td(struct cdnsp_device *pdev,
1773 struct cdnsp_request *preq, u32 enqd_len,
1774 u32 *trb_buff_len, struct cdnsp_segment *seg)
1776 struct device *dev = pdev->dev;
1777 unsigned int unalign;
1778 unsigned int max_pkt;
1781 max_pkt = usb_endpoint_maxp(preq->pep->endpoint.desc);
1782 unalign = (enqd_len + *trb_buff_len) % max_pkt;
1784 /* We got lucky, last normal TRB data on segment is packet aligned. */
1788 /* Is the last nornal TRB alignable by splitting it. */
1789 if (*trb_buff_len > unalign) {
1790 *trb_buff_len -= unalign;
1791 trace_cdnsp_bounce_align_td_split(preq, *trb_buff_len,
1792 enqd_len, 0, unalign);
1797 * We want enqd_len + trb_buff_len to sum up to a number aligned to
1798 * number which is divisible by the endpoint's wMaxPacketSize. IOW:
1799 * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0.
1801 new_buff_len = max_pkt - (enqd_len % max_pkt);
1803 if (new_buff_len > (preq->request.length - enqd_len))
1804 new_buff_len = (preq->request.length - enqd_len);
1806 /* Create a max max_pkt sized bounce buffer pointed to by last trb. */
1807 if (preq->direction) {
1808 sg_pcopy_to_buffer(preq->request.sg,
1809 preq->request.num_mapped_sgs,
1810 seg->bounce_buf, new_buff_len, enqd_len);
1811 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
1812 max_pkt, DMA_TO_DEVICE);
1814 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
1815 max_pkt, DMA_FROM_DEVICE);
1818 if (dma_mapping_error(dev, seg->bounce_dma)) {
1819 /* Try without aligning.*/
1821 "Failed mapping bounce buffer, not aligning\n");
1825 *trb_buff_len = new_buff_len;
1826 seg->bounce_len = new_buff_len;
1827 seg->bounce_offs = enqd_len;
1829 trace_cdnsp_bounce_map(preq, new_buff_len, enqd_len, seg->bounce_dma,
1833 * Bounce buffer successful aligned and seg->bounce_dma will be used
1834 * in transfer TRB as new transfer buffer address.
1839 int cdnsp_queue_bulk_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq)
1841 unsigned int enqd_len, block_len, trb_buff_len, full_len;
1842 unsigned int start_cycle, num_sgs = 0;
1843 struct cdnsp_generic_trb *start_trb;
1844 u32 field, length_field, remainder;
1845 struct scatterlist *sg = NULL;
1846 bool more_trbs_coming = true;
1847 bool need_zero_pkt = false;
1848 bool zero_len_trb = false;
1849 struct cdnsp_ring *ring;
1850 bool first_trb = true;
1851 unsigned int num_trbs;
1852 struct cdnsp_ep *pep;
1853 u64 addr, send_addr;
1856 ring = cdnsp_request_to_transfer_ring(pdev, preq);
1860 full_len = preq->request.length;
1862 if (preq->request.num_sgs) {
1863 num_sgs = preq->request.num_sgs;
1864 sg = preq->request.sg;
1865 addr = (u64)sg_dma_address(sg);
1866 block_len = sg_dma_len(sg);
1867 num_trbs = count_sg_trbs_needed(preq);
1869 num_trbs = count_trbs_needed(preq);
1870 addr = (u64)preq->request.dma;
1871 block_len = full_len;
1876 /* Deal with request.zero - need one more td/trb. */
1877 if (preq->request.zero && preq->request.length &&
1878 IS_ALIGNED(full_len, usb_endpoint_maxp(pep->endpoint.desc))) {
1879 need_zero_pkt = true;
1883 ret = cdnsp_prepare_transfer(pdev, preq, num_trbs);
1888 * Don't give the first TRB to the hardware (by toggling the cycle bit)
1889 * until we've finished creating all the other TRBs. The ring's cycle
1890 * state may change as we enqueue the other TRBs, so save it too.
1892 start_trb = &ring->enqueue->generic;
1893 start_cycle = ring->cycle_state;
1896 /* Queue the TRBs, even if they are zero-length */
1897 for (enqd_len = 0; zero_len_trb || first_trb || enqd_len < full_len;
1898 enqd_len += trb_buff_len) {
1899 field = TRB_TYPE(TRB_NORMAL);
1901 /* TRB buffer should not cross 64KB boundaries */
1902 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
1903 trb_buff_len = min(trb_buff_len, block_len);
1904 if (enqd_len + trb_buff_len > full_len)
1905 trb_buff_len = full_len - enqd_len;
1907 /* Don't change the cycle bit of the first TRB until later */
1910 if (start_cycle == 0)
1913 field |= ring->cycle_state;
1917 * Chain all the TRBs together; clear the chain bit in the last
1918 * TRB to indicate it's the last TRB in the chain.
1920 if (enqd_len + trb_buff_len < full_len || need_zero_pkt) {
1922 if (cdnsp_trb_is_link(ring->enqueue + 1)) {
1923 if (cdnsp_align_td(pdev, preq, enqd_len,
1926 send_addr = ring->enq_seg->bounce_dma;
1927 /* Assuming TD won't span 2 segs */
1928 preq->td.bounce_seg = ring->enq_seg;
1933 if (enqd_len + trb_buff_len >= full_len) {
1934 if (need_zero_pkt && zero_len_trb) {
1935 zero_len_trb = true;
1937 field &= ~TRB_CHAIN;
1939 more_trbs_coming = false;
1940 need_zero_pkt = false;
1941 preq->td.last_trb = ring->enqueue;
1945 /* Only set interrupt on short packet for OUT endpoints. */
1946 if (!preq->direction)
1949 /* Set the TRB length, TD size, and interrupter fields. */
1950 remainder = cdnsp_td_remainder(pdev, enqd_len, trb_buff_len,
1954 length_field = TRB_LEN(trb_buff_len) | TRB_TD_SIZE(remainder) |
1957 cdnsp_queue_trb(pdev, ring, more_trbs_coming | need_zero_pkt,
1958 lower_32_bits(send_addr),
1959 upper_32_bits(send_addr),
1963 addr += trb_buff_len;
1964 sent_len = trb_buff_len;
1965 while (sg && sent_len >= block_len) {
1968 sent_len -= block_len;
1971 block_len = sg_dma_len(sg);
1972 addr = (u64)sg_dma_address(sg);
1976 block_len -= sent_len;
1980 cdnsp_check_trb_math(preq, enqd_len);
1981 ret = cdnsp_giveback_first_trb(pdev, pep, preq->request.stream_id,
1982 start_cycle, start_trb);
1990 int cdnsp_queue_ctrl_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq)
1992 u32 field, length_field, remainder;
1993 struct cdnsp_ep *pep = preq->pep;
1994 struct cdnsp_ring *ep_ring;
1998 ep_ring = cdnsp_request_to_transfer_ring(pdev, preq);
2002 /* 1 TRB for data, 1 for status */
2003 num_trbs = (pdev->three_stage_setup) ? 2 : 1;
2005 ret = cdnsp_prepare_transfer(pdev, preq, num_trbs);
2009 /* If there's data, queue data TRBs */
2010 if (pdev->ep0_expect_in)
2011 field = TRB_TYPE(TRB_DATA) | TRB_IOC;
2013 field = TRB_ISP | TRB_TYPE(TRB_DATA) | TRB_IOC;
2015 if (preq->request.length > 0) {
2016 remainder = cdnsp_td_remainder(pdev, 0, preq->request.length,
2017 preq->request.length, preq, 1);
2019 length_field = TRB_LEN(preq->request.length) |
2020 TRB_TD_SIZE(remainder) | TRB_INTR_TARGET(0);
2022 if (pdev->ep0_expect_in)
2023 field |= TRB_DIR_IN;
2025 cdnsp_queue_trb(pdev, ep_ring, true,
2026 lower_32_bits(preq->request.dma),
2027 upper_32_bits(preq->request.dma), length_field,
2028 field | ep_ring->cycle_state |
2029 TRB_SETUPID(pdev->setup_id) |
2032 pdev->ep0_stage = CDNSP_DATA_STAGE;
2035 /* Save the DMA address of the last TRB in the TD. */
2036 preq->td.last_trb = ep_ring->enqueue;
2038 /* Queue status TRB. */
2039 if (preq->request.length == 0)
2040 field = ep_ring->cycle_state;
2042 field = (ep_ring->cycle_state ^ 1);
2044 if (preq->request.length > 0 && pdev->ep0_expect_in)
2045 field |= TRB_DIR_IN;
2047 if (pep->ep_state & EP0_HALTED_STATUS) {
2048 pep->ep_state &= ~EP0_HALTED_STATUS;
2049 field |= TRB_SETUPSTAT(TRB_SETUPSTAT_STALL);
2051 field |= TRB_SETUPSTAT(TRB_SETUPSTAT_ACK);
2054 cdnsp_queue_trb(pdev, ep_ring, false, 0, 0, TRB_INTR_TARGET(0),
2055 field | TRB_IOC | TRB_SETUPID(pdev->setup_id) |
2056 TRB_TYPE(TRB_STATUS) | pdev->setup_speed);
2058 cdnsp_ring_ep_doorbell(pdev, pep, preq->request.stream_id);
2063 int cdnsp_cmd_stop_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
2065 u32 ep_state = GET_EP_CTX_STATE(pep->out_ctx);
2068 if (ep_state == EP_STATE_STOPPED || ep_state == EP_STATE_DISABLED) {
2069 trace_cdnsp_ep_stopped_or_disabled(pep->out_ctx);
2073 cdnsp_queue_stop_endpoint(pdev, pep->idx);
2074 cdnsp_ring_cmd_db(pdev);
2075 ret = cdnsp_wait_for_cmd_compl(pdev);
2077 trace_cdnsp_handle_cmd_stop_ep(pep->out_ctx);
2080 pep->ep_state |= EP_STOPPED;
2084 int cdnsp_cmd_flush_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
2088 cdnsp_queue_flush_endpoint(pdev, pep->idx);
2089 cdnsp_ring_cmd_db(pdev);
2090 ret = cdnsp_wait_for_cmd_compl(pdev);
2092 trace_cdnsp_handle_cmd_flush_ep(pep->out_ctx);
2098 * The transfer burst count field of the isochronous TRB defines the number of
2099 * bursts that are required to move all packets in this TD. Only SuperSpeed
2100 * devices can burst up to bMaxBurst number of packets per service interval.
2101 * This field is zero based, meaning a value of zero in the field means one
2102 * burst. Basically, for everything but SuperSpeed devices, this field will be
2105 static unsigned int cdnsp_get_burst_count(struct cdnsp_device *pdev,
2106 struct cdnsp_request *preq,
2107 unsigned int total_packet_count)
2109 unsigned int max_burst;
2111 if (pdev->gadget.speed < USB_SPEED_SUPER)
2114 max_burst = preq->pep->endpoint.comp_desc->bMaxBurst;
2115 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1;
2119 * Returns the number of packets in the last "burst" of packets. This field is
2120 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
2121 * the last burst packet count is equal to the total number of packets in the
2122 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
2123 * must contain (bMaxBurst + 1) number of packets, but the last burst can
2124 * contain 1 to (bMaxBurst + 1) packets.
2127 cdnsp_get_last_burst_packet_count(struct cdnsp_device *pdev,
2128 struct cdnsp_request *preq,
2129 unsigned int total_packet_count)
2131 unsigned int max_burst;
2132 unsigned int residue;
2134 if (pdev->gadget.speed >= USB_SPEED_SUPER) {
2135 /* bMaxBurst is zero based: 0 means 1 packet per burst. */
2136 max_burst = preq->pep->endpoint.comp_desc->bMaxBurst;
2137 residue = total_packet_count % (max_burst + 1);
2140 * If residue is zero, the last burst contains (max_burst + 1)
2141 * number of packets, but the TLBPC field is zero-based.
2148 if (total_packet_count == 0)
2151 return total_packet_count - 1;
2154 /* Queue function isoc transfer */
2155 static int cdnsp_queue_isoc_tx(struct cdnsp_device *pdev,
2156 struct cdnsp_request *preq)
2158 int trb_buff_len, td_len, td_remain_len, ret;
2159 unsigned int burst_count, last_burst_pkt;
2160 unsigned int total_pkt_count, max_pkt;
2161 struct cdnsp_generic_trb *start_trb;
2162 bool more_trbs_coming = true;
2163 struct cdnsp_ring *ep_ring;
2164 int running_total = 0;
2165 u32 field, length_field;
2171 ep_ring = preq->pep->ring;
2172 start_trb = &ep_ring->enqueue->generic;
2173 start_cycle = ep_ring->cycle_state;
2174 td_len = preq->request.length;
2175 addr = (u64)preq->request.dma;
2176 td_remain_len = td_len;
2178 max_pkt = usb_endpoint_maxp(preq->pep->endpoint.desc);
2179 total_pkt_count = DIV_ROUND_UP(td_len, max_pkt);
2181 /* A zero-length transfer still involves at least one packet. */
2182 if (total_pkt_count == 0)
2185 burst_count = cdnsp_get_burst_count(pdev, preq, total_pkt_count);
2186 last_burst_pkt = cdnsp_get_last_burst_packet_count(pdev, preq,
2188 trbs_per_td = count_isoc_trbs_needed(preq);
2190 ret = cdnsp_prepare_transfer(pdev, preq, trbs_per_td);
2195 * Set isoc specific data for the first TRB in a TD.
2196 * Prevent HW from getting the TRBs by keeping the cycle state
2197 * inverted in the first TDs isoc TRB.
2199 field = TRB_TYPE(TRB_ISOC) | TRB_TLBPC(last_burst_pkt) |
2200 start_cycle ? 0 : 1 | TRB_SIA | TRB_TBC(burst_count);
2202 /* Fill the rest of the TRB fields, and remaining normal TRBs. */
2203 for (i = 0; i < trbs_per_td; i++) {
2206 /* Calculate TRB length. */
2207 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
2208 if (trb_buff_len > td_remain_len)
2209 trb_buff_len = td_remain_len;
2211 /* Set the TRB length, TD size, & interrupter fields. */
2212 remainder = cdnsp_td_remainder(pdev, running_total,
2213 trb_buff_len, td_len, preq,
2216 length_field = TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0);
2218 /* Only first TRB is isoc, overwrite otherwise. */
2220 field = TRB_TYPE(TRB_NORMAL) | ep_ring->cycle_state;
2221 length_field |= TRB_TD_SIZE(remainder);
2223 length_field |= TRB_TD_SIZE_TBC(burst_count);
2226 /* Only set interrupt on short packet for OUT EPs. */
2227 if (usb_endpoint_dir_out(preq->pep->endpoint.desc))
2230 /* Set the chain bit for all except the last TRB. */
2231 if (i < trbs_per_td - 1) {
2232 more_trbs_coming = true;
2235 more_trbs_coming = false;
2236 preq->td.last_trb = ep_ring->enqueue;
2240 cdnsp_queue_trb(pdev, ep_ring, more_trbs_coming,
2241 lower_32_bits(addr), upper_32_bits(addr),
2242 length_field, field);
2244 running_total += trb_buff_len;
2245 addr += trb_buff_len;
2246 td_remain_len -= trb_buff_len;
2249 /* Check TD length */
2250 if (running_total != td_len) {
2251 dev_err(pdev->dev, "ISOC TD length unmatch\n");
2256 cdnsp_giveback_first_trb(pdev, preq->pep, preq->request.stream_id,
2257 start_cycle, start_trb);
2262 /* Clean up a partially enqueued isoc transfer. */
2263 list_del_init(&preq->td.td_list);
2267 * Use the first TD as a temporary variable to turn the TDs we've
2268 * queued into No-ops with a software-owned cycle bit.
2269 * That way the hardware won't accidentally start executing bogus TDs
2270 * when we partially overwrite them.
2271 * td->first_trb and td->start_seg are already set.
2273 preq->td.last_trb = ep_ring->enqueue;
2274 /* Every TRB except the first & last will have its cycle bit flipped. */
2275 cdnsp_td_to_noop(pdev, ep_ring, &preq->td, true);
2277 /* Reset the ring enqueue back to the first TRB and its cycle bit. */
2278 ep_ring->enqueue = preq->td.first_trb;
2279 ep_ring->enq_seg = preq->td.start_seg;
2280 ep_ring->cycle_state = start_cycle;
2284 int cdnsp_queue_isoc_tx_prepare(struct cdnsp_device *pdev,
2285 struct cdnsp_request *preq)
2287 struct cdnsp_ring *ep_ring;
2292 ep_ring = preq->pep->ring;
2293 ep_state = GET_EP_CTX_STATE(preq->pep->out_ctx);
2294 num_trbs = count_isoc_trbs_needed(preq);
2297 * Check the ring to guarantee there is enough room for the whole
2298 * request. Do not insert any td of the USB Request to the ring if the
2301 ret = cdnsp_prepare_ring(pdev, ep_ring, ep_state, num_trbs, GFP_ATOMIC);
2305 return cdnsp_queue_isoc_tx(pdev, preq);
2308 /**** Command Ring Operations ****/
2310 * Generic function for queuing a command TRB on the command ring.
2311 * Driver queue only one command to ring in the moment.
2313 static void cdnsp_queue_command(struct cdnsp_device *pdev,
2319 cdnsp_prepare_ring(pdev, pdev->cmd_ring, EP_STATE_RUNNING, 1,
2322 pdev->cmd.command_trb = pdev->cmd_ring->enqueue;
2324 cdnsp_queue_trb(pdev, pdev->cmd_ring, false, field1, field2,
2325 field3, field4 | pdev->cmd_ring->cycle_state);
2328 /* Queue a slot enable or disable request on the command ring */
2329 void cdnsp_queue_slot_control(struct cdnsp_device *pdev, u32 trb_type)
2331 cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(trb_type) |
2332 SLOT_ID_FOR_TRB(pdev->slot_id));
2335 /* Queue an address device command TRB */
2336 void cdnsp_queue_address_device(struct cdnsp_device *pdev,
2337 dma_addr_t in_ctx_ptr,
2338 enum cdnsp_setup_dev setup)
2340 cdnsp_queue_command(pdev, lower_32_bits(in_ctx_ptr),
2341 upper_32_bits(in_ctx_ptr), 0,
2342 TRB_TYPE(TRB_ADDR_DEV) |
2343 SLOT_ID_FOR_TRB(pdev->slot_id) |
2344 (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0));
2347 /* Queue a reset device command TRB */
2348 void cdnsp_queue_reset_device(struct cdnsp_device *pdev)
2350 cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(TRB_RESET_DEV) |
2351 SLOT_ID_FOR_TRB(pdev->slot_id));
2354 /* Queue a configure endpoint command TRB */
2355 void cdnsp_queue_configure_endpoint(struct cdnsp_device *pdev,
2356 dma_addr_t in_ctx_ptr)
2358 cdnsp_queue_command(pdev, lower_32_bits(in_ctx_ptr),
2359 upper_32_bits(in_ctx_ptr), 0,
2360 TRB_TYPE(TRB_CONFIG_EP) |
2361 SLOT_ID_FOR_TRB(pdev->slot_id));
2365 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
2366 * activity on an endpoint that is about to be suspended.
2368 void cdnsp_queue_stop_endpoint(struct cdnsp_device *pdev, unsigned int ep_index)
2370 cdnsp_queue_command(pdev, 0, 0, 0, SLOT_ID_FOR_TRB(pdev->slot_id) |
2371 EP_ID_FOR_TRB(ep_index) | TRB_TYPE(TRB_STOP_RING));
2374 /* Set Transfer Ring Dequeue Pointer command. */
2375 void cdnsp_queue_new_dequeue_state(struct cdnsp_device *pdev,
2376 struct cdnsp_ep *pep,
2377 struct cdnsp_dequeue_state *deq_state)
2379 u32 trb_stream_id = STREAM_ID_FOR_TRB(deq_state->stream_id);
2380 u32 trb_slot_id = SLOT_ID_FOR_TRB(pdev->slot_id);
2381 u32 type = TRB_TYPE(TRB_SET_DEQ);
2385 addr = cdnsp_trb_virt_to_dma(deq_state->new_deq_seg,
2386 deq_state->new_deq_ptr);
2388 if (deq_state->stream_id)
2389 trb_sct = SCT_FOR_TRB(SCT_PRI_TR);
2391 cdnsp_queue_command(pdev, lower_32_bits(addr) | trb_sct |
2392 deq_state->new_cycle_state, upper_32_bits(addr),
2393 trb_stream_id, trb_slot_id |
2394 EP_ID_FOR_TRB(pep->idx) | type);
2397 void cdnsp_queue_reset_ep(struct cdnsp_device *pdev, unsigned int ep_index)
2399 return cdnsp_queue_command(pdev, 0, 0, 0,
2400 SLOT_ID_FOR_TRB(pdev->slot_id) |
2401 EP_ID_FOR_TRB(ep_index) |
2402 TRB_TYPE(TRB_RESET_EP));
2406 * Queue a halt endpoint request on the command ring.
2408 void cdnsp_queue_halt_endpoint(struct cdnsp_device *pdev, unsigned int ep_index)
2410 cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(TRB_HALT_ENDPOINT) |
2411 SLOT_ID_FOR_TRB(pdev->slot_id) |
2412 EP_ID_FOR_TRB(ep_index));
2416 * Queue a flush endpoint request on the command ring.
2418 void cdnsp_queue_flush_endpoint(struct cdnsp_device *pdev,
2419 unsigned int ep_index)
2421 cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(TRB_FLUSH_ENDPOINT) |
2422 SLOT_ID_FOR_TRB(pdev->slot_id) |
2423 EP_ID_FOR_TRB(ep_index));
2426 void cdnsp_force_header_wakeup(struct cdnsp_device *pdev, int intf_num)
2430 lo = TRB_FH_TO_PACKET_TYPE(TRB_FH_TR_PACKET) |
2431 TRB_FH_TO_DEVICE_ADDRESS(pdev->device_address);
2432 mid = TRB_FH_TR_PACKET_DEV_NOT |
2433 TRB_FH_TO_NOT_TYPE(TRB_FH_TR_PACKET_FUNCTION_WAKE) |
2434 TRB_FH_TO_INTERFACE(intf_num);
2436 cdnsp_queue_command(pdev, lo, mid, 0,
2437 TRB_TYPE(TRB_FORCE_HEADER) | SET_PORT_ID(2));