1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence CDNSP DRD Driver.
5 * Copyright (C) 2020 Cadence.
7 * Author: Pawel Laszczak <pawell@cadence.com>
11 #include <linux/moduleparam.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/module.h>
14 #include <linux/iopoll.h>
15 #include <linux/delay.h>
16 #include <linux/log2.h>
17 #include <linux/slab.h>
18 #include <linux/string_choices.h>
19 #include <linux/pci.h>
20 #include <linux/irq.h>
21 #include <linux/dmi.h>
24 #include "gadget-export.h"
26 #include "cdnsp-gadget.h"
27 #include "cdnsp-trace.h"
29 unsigned int cdnsp_port_speed(unsigned int port_status)
31 /*Detect gadget speed based on PORTSC register*/
32 if (DEV_SUPERSPEEDPLUS(port_status) ||
33 DEV_SSP_GEN1x2(port_status) || DEV_SSP_GEN2x2(port_status))
34 return USB_SPEED_SUPER_PLUS;
35 else if (DEV_SUPERSPEED(port_status))
36 return USB_SPEED_SUPER;
37 else if (DEV_HIGHSPEED(port_status))
38 return USB_SPEED_HIGH;
39 else if (DEV_FULLSPEED(port_status))
40 return USB_SPEED_FULL;
42 /* If device is detached then speed will be USB_SPEED_UNKNOWN.*/
43 return USB_SPEED_UNKNOWN;
47 * Given a port state, this function returns a value that would result in the
48 * port being in the same state, if the value was written to the port status
50 * Save Read Only (RO) bits and save read/write bits where
51 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
52 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
54 u32 cdnsp_port_state_to_neutral(u32 state)
56 /* Save read-only status and port state. */
57 return (state & CDNSP_PORT_RO) | (state & CDNSP_PORT_RWS);
61 * cdnsp_find_next_ext_cap - Find the offset of the extended capabilities
62 * with capability ID id.
63 * @base: PCI MMIO registers base address.
64 * @start: Address at which to start looking, (0 or HCC_PARAMS to start at
66 * @id: Extended capability ID to search for.
68 * Returns the offset of the next matching extended capability structure.
69 * Some capabilities can occur several times,
70 * e.g., the EXT_CAPS_PROTOCOL, and this provides a way to find them all.
72 int cdnsp_find_next_ext_cap(void __iomem *base, u32 start, int id)
78 if (!start || start == HCC_PARAMS_OFFSET) {
79 val = readl(base + HCC_PARAMS_OFFSET);
83 offset = HCC_EXT_CAPS(val) << 2;
89 val = readl(base + offset);
93 if (EXT_CAPS_ID(val) == id && offset != start)
96 next = EXT_CAPS_NEXT(val);
103 void cdnsp_set_link_state(struct cdnsp_device *pdev,
104 __le32 __iomem *port_regs,
110 temp = readl(port_regs);
111 temp = cdnsp_port_state_to_neutral(temp);
112 temp |= PORT_WKCONN_E | PORT_WKDISC_E;
113 writel(temp, port_regs);
115 temp &= ~PORT_PLS_MASK;
116 temp |= PORT_LINK_STROBE | link_state;
118 if (pdev->active_port)
119 port_num = pdev->active_port->port_num;
121 trace_cdnsp_handle_port_status(port_num, readl(port_regs));
122 writel(temp, port_regs);
123 trace_cdnsp_link_state_changed(port_num, readl(port_regs));
126 static void cdnsp_disable_port(struct cdnsp_device *pdev,
127 __le32 __iomem *port_regs)
129 u32 temp = cdnsp_port_state_to_neutral(readl(port_regs));
131 writel(temp | PORT_PED, port_regs);
134 static void cdnsp_clear_port_change_bit(struct cdnsp_device *pdev,
135 __le32 __iomem *port_regs)
137 u32 portsc = readl(port_regs);
139 writel(cdnsp_port_state_to_neutral(portsc) |
140 (portsc & PORT_CHANGE_BITS), port_regs);
143 static void cdnsp_set_apb_timeout_value(struct cdnsp_device *pdev)
145 struct cdns *cdns = dev_get_drvdata(pdev->dev);
151 if (!cdns->override_apb_timeout)
154 base = &pdev->cap_regs->hc_capbase;
155 offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
156 reg = base + offset + REG_CHICKEN_BITS_3_OFFSET;
158 val = le32_to_cpu(readl(reg));
159 val = CHICKEN_APB_TIMEOUT_SET(val, cdns->override_apb_timeout);
160 writel(cpu_to_le32(val), reg);
163 static void cdnsp_set_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
169 base = &pdev->cap_regs->hc_capbase;
170 offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
171 reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
173 bit = readl(reg) | bit;
177 static void cdnsp_clear_chicken_bits_2(struct cdnsp_device *pdev, u32 bit)
183 base = &pdev->cap_regs->hc_capbase;
184 offset = cdnsp_find_next_ext_cap(base, offset, D_XEC_PRE_REGS_CAP);
185 reg = base + offset + REG_CHICKEN_BITS_2_OFFSET;
187 bit = readl(reg) & ~bit;
192 * Disable interrupts and begin the controller halting process.
194 static void cdnsp_quiesce(struct cdnsp_device *pdev)
200 mask = ~(u32)(CDNSP_IRQS);
202 halted = readl(&pdev->op_regs->status) & STS_HALT;
204 mask &= ~(CMD_R_S | CMD_DEVEN);
206 cmd = readl(&pdev->op_regs->command);
208 writel(cmd, &pdev->op_regs->command);
212 * Force controller into halt state.
214 * Disable any IRQs and clear the run/stop bit.
215 * Controller will complete any current and actively pipelined transactions, and
216 * should halt within 16 ms of the run/stop bit being cleared.
217 * Read controller Halted bit in the status register to see when the
218 * controller is finished.
220 int cdnsp_halt(struct cdnsp_device *pdev)
227 ret = readl_poll_timeout_atomic(&pdev->op_regs->status, val,
229 CDNSP_MAX_HALT_USEC);
231 dev_err(pdev->dev, "ERROR: Device halt failed\n");
235 pdev->cdnsp_state |= CDNSP_STATE_HALTED;
241 * device controller died, register read returns 0xffffffff, or command never
244 void cdnsp_died(struct cdnsp_device *pdev)
246 dev_err(pdev->dev, "ERROR: CDNSP controller not responding\n");
247 pdev->cdnsp_state |= CDNSP_STATE_DYING;
252 * Set the run bit and wait for the device to be running.
254 static int cdnsp_start(struct cdnsp_device *pdev)
259 temp = readl(&pdev->op_regs->command);
260 temp |= (CMD_R_S | CMD_DEVEN);
261 writel(temp, &pdev->op_regs->command);
263 pdev->cdnsp_state = 0;
266 * Wait for the STS_HALT Status bit to be 0 to indicate the device is
269 ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
270 !(temp & STS_HALT), 1,
271 CDNSP_MAX_HALT_USEC);
273 pdev->cdnsp_state = CDNSP_STATE_DYING;
274 dev_err(pdev->dev, "ERROR: Controller run failed\n");
281 * Reset a halted controller.
283 * This resets pipelines, timers, counters, state machines, etc.
284 * Transactions will be terminated immediately, and operational registers
285 * will be set to their defaults.
287 int cdnsp_reset(struct cdnsp_device *pdev)
293 temp = readl(&pdev->op_regs->status);
295 if (temp == ~(u32)0) {
296 dev_err(pdev->dev, "Device not accessible, reset failed.\n");
300 if ((temp & STS_HALT) == 0) {
301 dev_err(pdev->dev, "Controller not halted, aborting reset.\n");
305 command = readl(&pdev->op_regs->command);
306 command |= CMD_RESET;
307 writel(command, &pdev->op_regs->command);
309 ret = readl_poll_timeout_atomic(&pdev->op_regs->command, temp,
310 !(temp & CMD_RESET), 1,
313 dev_err(pdev->dev, "ERROR: Controller reset failed\n");
318 * CDNSP cannot write any doorbells or operational registers other
319 * than status until the "Controller Not Ready" flag is cleared.
321 ret = readl_poll_timeout_atomic(&pdev->op_regs->status, temp,
322 !(temp & STS_CNR), 1,
326 dev_err(pdev->dev, "ERROR: Controller not ready to work\n");
330 dev_dbg(pdev->dev, "Controller ready to work");
336 * cdnsp_get_endpoint_index - Find the index for an endpoint given its
337 * descriptor.Use the return value to right shift 1 for the bitmask.
339 * Index = (epnum * 2) + direction - 1,
340 * where direction = 0 for OUT, 1 for IN.
341 * For control endpoints, the IN index is used (OUT index is unused), so
342 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
345 cdnsp_get_endpoint_index(const struct usb_endpoint_descriptor *desc)
347 unsigned int index = (unsigned int)usb_endpoint_num(desc);
349 if (usb_endpoint_xfer_control(desc))
352 return (index * 2) + (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
356 * Find the flag for this endpoint (for use in the control context). Use the
357 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
361 cdnsp_get_endpoint_flag(const struct usb_endpoint_descriptor *desc)
363 return 1 << (cdnsp_get_endpoint_index(desc) + 1);
366 int cdnsp_ep_enqueue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
368 struct cdnsp_device *pdev = pep->pdev;
369 struct usb_request *request;
372 if (preq->epnum == 0 && !list_empty(&pep->pending_list)) {
373 trace_cdnsp_request_enqueue_busy(preq);
377 request = &preq->request;
379 request->status = -EINPROGRESS;
380 preq->direction = pep->direction;
381 preq->epnum = pep->number;
384 ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->direction);
386 trace_cdnsp_request_enqueue_error(preq);
390 list_add_tail(&preq->list, &pep->pending_list);
392 trace_cdnsp_request_enqueue(preq);
394 switch (usb_endpoint_type(pep->endpoint.desc)) {
395 case USB_ENDPOINT_XFER_CONTROL:
396 ret = cdnsp_queue_ctrl_tx(pdev, preq);
398 case USB_ENDPOINT_XFER_BULK:
399 case USB_ENDPOINT_XFER_INT:
400 ret = cdnsp_queue_bulk_tx(pdev, preq);
402 case USB_ENDPOINT_XFER_ISOC:
403 ret = cdnsp_queue_isoc_tx(pdev, preq);
412 usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
414 list_del(&preq->list);
415 trace_cdnsp_request_enqueue_error(preq);
421 * Remove the request's TD from the endpoint ring. This may cause the
422 * controller to stop USB transfers, potentially stopping in the middle of a
423 * TRB buffer. The controller should pick up where it left off in the TD,
424 * unless a Set Transfer Ring Dequeue Pointer is issued.
426 * The TRBs that make up the buffers for the canceled request will be "removed"
427 * from the ring. Since the ring is a contiguous structure, they can't be
428 * physically removed. Instead, there are two options:
430 * 1) If the controller is in the middle of processing the request to be
431 * canceled, we simply move the ring's dequeue pointer past those TRBs
432 * using the Set Transfer Ring Dequeue Pointer command. This will be
433 * the common case, when drivers timeout on the last submitted request
434 * and attempt to cancel.
436 * 2) If the controller is in the middle of a different TD, we turn the TRBs
437 * into a series of 1-TRB transfer no-op TDs. No-ops shouldn't be chained.
438 * The controller will need to invalidate the any TRBs it has cached after
439 * the stop endpoint command.
441 * 3) The TD may have completed by the time the Stop Endpoint Command
442 * completes, so software needs to handle that case too.
445 int cdnsp_ep_dequeue(struct cdnsp_ep *pep, struct cdnsp_request *preq)
447 struct cdnsp_device *pdev = pep->pdev;
451 trace_cdnsp_request_dequeue(preq);
453 if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_RUNNING)
454 ret_stop = cdnsp_cmd_stop_ep(pdev, pep);
456 ret_rem = cdnsp_remove_request(pdev, preq, pep);
458 return ret_rem ? ret_rem : ret_stop;
461 static void cdnsp_zero_in_ctx(struct cdnsp_device *pdev)
463 struct cdnsp_input_control_ctx *ctrl_ctx;
464 struct cdnsp_slot_ctx *slot_ctx;
465 struct cdnsp_ep_ctx *ep_ctx;
468 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
471 * When a device's add flag and drop flag are zero, any subsequent
472 * configure endpoint command will leave that endpoint's state
473 * untouched. Make sure we don't leave any old state in the input
476 ctrl_ctx->drop_flags = 0;
477 ctrl_ctx->add_flags = 0;
478 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
479 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
481 /* Endpoint 0 is always valid */
482 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
483 for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i) {
484 ep_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, i);
486 ep_ctx->ep_info2 = 0;
492 /* Issue a configure endpoint command and wait for it to finish. */
493 static int cdnsp_configure_endpoint(struct cdnsp_device *pdev)
497 cdnsp_queue_configure_endpoint(pdev, pdev->cmd.in_ctx->dma);
498 cdnsp_ring_cmd_db(pdev);
499 ret = cdnsp_wait_for_cmd_compl(pdev);
502 "ERR: unexpected command completion code 0x%x.\n", ret);
509 static void cdnsp_invalidate_ep_events(struct cdnsp_device *pdev,
510 struct cdnsp_ep *pep)
512 struct cdnsp_segment *segment;
513 union cdnsp_trb *event;
517 event = pdev->event_ring->dequeue;
518 segment = pdev->event_ring->deq_seg;
519 cycle_state = pdev->event_ring->cycle_state;
522 data = le32_to_cpu(event->trans_event.flags);
524 /* Check the owner of the TRB. */
525 if ((data & TRB_CYCLE) != cycle_state)
528 if (TRB_FIELD_TO_TYPE(data) == TRB_TRANSFER &&
529 TRB_TO_EP_ID(data) == (pep->idx + 1)) {
530 data |= TRB_EVENT_INVALIDATE;
531 event->trans_event.flags = cpu_to_le32(data);
534 if (cdnsp_last_trb_on_seg(segment, event)) {
536 segment = pdev->event_ring->deq_seg->next;
537 event = segment->trbs;
544 int cdnsp_wait_for_cmd_compl(struct cdnsp_device *pdev)
546 struct cdnsp_segment *event_deq_seg;
547 union cdnsp_trb *cmd_trb;
548 dma_addr_t cmd_deq_dma;
549 union cdnsp_trb *event;
556 cmd_trb = pdev->cmd.command_trb;
557 pdev->cmd.status = 0;
559 trace_cdnsp_cmd_wait_for_compl(pdev->cmd_ring, &cmd_trb->generic);
561 ret = readl_poll_timeout_atomic(&pdev->op_regs->cmd_ring, val,
562 !CMD_RING_BUSY(val), 1,
565 dev_err(pdev->dev, "ERR: Timeout while waiting for command\n");
566 trace_cdnsp_cmd_timeout(pdev->cmd_ring, &cmd_trb->generic);
567 pdev->cdnsp_state = CDNSP_STATE_DYING;
571 event = pdev->event_ring->dequeue;
572 event_deq_seg = pdev->event_ring->deq_seg;
573 cycle_state = pdev->event_ring->cycle_state;
575 cmd_deq_dma = cdnsp_trb_virt_to_dma(pdev->cmd_ring->deq_seg, cmd_trb);
580 flags = le32_to_cpu(event->event_cmd.flags);
582 /* Check the owner of the TRB. */
583 if ((flags & TRB_CYCLE) != cycle_state) {
585 * Give some extra time to get chance controller
586 * to finish command before returning error code.
587 * Checking CMD_RING_BUSY is not sufficient because
588 * this bit is cleared to '0' when the Command
589 * Descriptor has been executed by controller
590 * and not when command completion event has
591 * be added to event ring.
601 cmd_dma = le64_to_cpu(event->event_cmd.cmd_trb);
604 * Check whether the completion event is for last queued
607 if (TRB_FIELD_TO_TYPE(flags) != TRB_COMPLETION ||
608 cmd_dma != (u64)cmd_deq_dma) {
609 if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
614 if (cdnsp_last_trb_on_ring(pdev->event_ring,
615 event_deq_seg, event))
618 event_deq_seg = event_deq_seg->next;
619 event = event_deq_seg->trbs;
623 trace_cdnsp_handle_command(pdev->cmd_ring, &cmd_trb->generic);
625 pdev->cmd.status = GET_COMP_CODE(le32_to_cpu(event->event_cmd.status));
626 if (pdev->cmd.status == COMP_SUCCESS)
629 return -pdev->cmd.status;
633 int cdnsp_halt_endpoint(struct cdnsp_device *pdev,
634 struct cdnsp_ep *pep,
639 trace_cdnsp_ep_halt(value ? "Set" : "Clear");
641 ret = cdnsp_cmd_stop_ep(pdev, pep);
646 if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_STOPPED) {
647 cdnsp_queue_halt_endpoint(pdev, pep->idx);
648 cdnsp_ring_cmd_db(pdev);
649 ret = cdnsp_wait_for_cmd_compl(pdev);
652 pep->ep_state |= EP_HALTED;
654 cdnsp_queue_reset_ep(pdev, pep->idx);
655 cdnsp_ring_cmd_db(pdev);
656 ret = cdnsp_wait_for_cmd_compl(pdev);
657 trace_cdnsp_handle_cmd_reset_ep(pep->out_ctx);
662 pep->ep_state &= ~EP_HALTED;
664 if (pep->idx != 0 && !(pep->ep_state & EP_WEDGE))
665 cdnsp_ring_doorbell_for_active_rings(pdev, pep);
667 pep->ep_state &= ~EP_WEDGE;
673 static int cdnsp_update_eps_configuration(struct cdnsp_device *pdev,
674 struct cdnsp_ep *pep)
676 struct cdnsp_input_control_ctx *ctrl_ctx;
677 struct cdnsp_slot_ctx *slot_ctx;
682 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
684 /* Don't issue the command if there's no endpoints to update. */
685 if (ctrl_ctx->add_flags == 0 && ctrl_ctx->drop_flags == 0)
688 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
689 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
690 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
692 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
693 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
694 for (i = CDNSP_ENDPOINTS_NUM; i >= 1; i--) {
695 __le32 le32 = cpu_to_le32(BIT(i));
697 if ((pdev->eps[i - 1].ring && !(ctrl_ctx->drop_flags & le32)) ||
698 (ctrl_ctx->add_flags & le32) || i == 1) {
699 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
700 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
705 ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
707 if ((ctrl_ctx->add_flags != cpu_to_le32(SLOT_FLAG) &&
708 ep_sts == EP_STATE_DISABLED) ||
709 (ep_sts != EP_STATE_DISABLED && ctrl_ctx->drop_flags))
710 ret = cdnsp_configure_endpoint(pdev);
712 trace_cdnsp_configure_endpoint(cdnsp_get_slot_ctx(&pdev->out_ctx));
713 trace_cdnsp_handle_cmd_config_ep(pep->out_ctx);
715 cdnsp_zero_in_ctx(pdev);
721 * This submits a Reset Device Command, which will set the device state to 0,
722 * set the device address to 0, and disable all the endpoints except the default
723 * control endpoint. The USB core should come back and call
724 * cdnsp_setup_device(), and then re-set up the configuration.
726 int cdnsp_reset_device(struct cdnsp_device *pdev)
728 struct cdnsp_slot_ctx *slot_ctx;
732 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
733 slot_ctx->dev_info = 0;
734 pdev->device_address = 0;
736 /* If device is not setup, there is no point in resetting it. */
737 slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
738 slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
739 trace_cdnsp_reset_device(slot_ctx);
741 if (slot_state <= SLOT_STATE_DEFAULT &&
742 pdev->eps[0].ep_state & EP_HALTED) {
743 cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
747 * During Reset Device command controller shall transition the
748 * endpoint ep0 to the Running State.
750 pdev->eps[0].ep_state &= ~(EP_STOPPED | EP_HALTED);
751 pdev->eps[0].ep_state |= EP_ENABLED;
753 if (slot_state <= SLOT_STATE_DEFAULT)
756 cdnsp_queue_reset_device(pdev);
757 cdnsp_ring_cmd_db(pdev);
758 ret = cdnsp_wait_for_cmd_compl(pdev);
761 * After Reset Device command all not default endpoints
762 * are in Disabled state.
764 for (i = 1; i < CDNSP_ENDPOINTS_NUM; ++i)
765 pdev->eps[i].ep_state |= EP_STOPPED | EP_UNCONFIGURED;
767 trace_cdnsp_handle_cmd_reset_dev(slot_ctx);
770 dev_err(pdev->dev, "Reset device failed with error code %d",
777 * Sets the MaxPStreams field and the Linear Stream Array field.
778 * Sets the dequeue pointer to the stream context array.
780 static void cdnsp_setup_streams_ep_input_ctx(struct cdnsp_device *pdev,
781 struct cdnsp_ep_ctx *ep_ctx,
782 struct cdnsp_stream_info *stream_info)
784 u32 max_primary_streams;
786 /* MaxPStreams is the number of stream context array entries, not the
787 * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
788 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
790 max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
791 ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
792 ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
794 ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma);
798 * The drivers use this function to prepare a bulk endpoints to use streams.
800 * Don't allow the call to succeed if endpoint only supports one stream
801 * (which means it doesn't support streams at all).
803 int cdnsp_alloc_streams(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
805 unsigned int num_streams = usb_ss_max_streams(pep->endpoint.comp_desc);
806 unsigned int num_stream_ctxs;
809 if (num_streams == 0)
812 if (num_streams > STREAM_NUM_STREAMS)
816 * Add two to the number of streams requested to account for
817 * stream 0 that is reserved for controller usage and one additional
818 * for TASK SET FULL response.
822 /* The stream context array size must be a power of two */
823 num_stream_ctxs = roundup_pow_of_two(num_streams);
825 trace_cdnsp_stream_number(pep, num_stream_ctxs, num_streams);
827 ret = cdnsp_alloc_stream_info(pdev, pep, num_stream_ctxs, num_streams);
831 cdnsp_setup_streams_ep_input_ctx(pdev, pep->in_ctx, &pep->stream_info);
833 pep->ep_state |= EP_HAS_STREAMS;
834 pep->stream_info.td_count = 0;
835 pep->stream_info.first_prime_det = 0;
837 /* Subtract 1 for stream 0, which drivers can't use. */
838 return num_streams - 1;
841 int cdnsp_disable_slot(struct cdnsp_device *pdev)
845 cdnsp_queue_slot_control(pdev, TRB_DISABLE_SLOT);
846 cdnsp_ring_cmd_db(pdev);
847 ret = cdnsp_wait_for_cmd_compl(pdev);
850 pdev->active_port = NULL;
852 trace_cdnsp_handle_cmd_disable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
854 memset(pdev->in_ctx.bytes, 0, CDNSP_CTX_SIZE);
855 memset(pdev->out_ctx.bytes, 0, CDNSP_CTX_SIZE);
860 int cdnsp_enable_slot(struct cdnsp_device *pdev)
862 struct cdnsp_slot_ctx *slot_ctx;
866 /* If device is not setup, there is no point in resetting it */
867 slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
868 slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
870 if (slot_state != SLOT_STATE_DISABLED)
873 cdnsp_queue_slot_control(pdev, TRB_ENABLE_SLOT);
874 cdnsp_ring_cmd_db(pdev);
875 ret = cdnsp_wait_for_cmd_compl(pdev);
882 trace_cdnsp_handle_cmd_enable_slot(cdnsp_get_slot_ctx(&pdev->out_ctx));
888 * Issue an Address Device command with BSR=0 if setup is SETUP_CONTEXT_ONLY
889 * or with BSR = 1 if set_address is SETUP_CONTEXT_ADDRESS.
891 int cdnsp_setup_device(struct cdnsp_device *pdev, enum cdnsp_setup_dev setup)
893 struct cdnsp_input_control_ctx *ctrl_ctx;
894 struct cdnsp_slot_ctx *slot_ctx;
898 if (!pdev->slot_id) {
899 trace_cdnsp_slot_id("incorrect");
903 if (!pdev->active_port->port_num)
906 slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
907 dev_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
909 if (setup == SETUP_CONTEXT_ONLY && dev_state == SLOT_STATE_DEFAULT) {
910 trace_cdnsp_slot_already_in_default(slot_ctx);
914 slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
915 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
917 if (!slot_ctx->dev_info || dev_state == SLOT_STATE_DEFAULT) {
918 ret = cdnsp_setup_addressable_priv_dev(pdev);
923 cdnsp_copy_ep0_dequeue_into_input_ctx(pdev);
925 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
926 ctrl_ctx->drop_flags = 0;
928 trace_cdnsp_setup_device_slot(slot_ctx);
930 cdnsp_queue_address_device(pdev, pdev->in_ctx.dma, setup);
931 cdnsp_ring_cmd_db(pdev);
932 ret = cdnsp_wait_for_cmd_compl(pdev);
934 trace_cdnsp_handle_cmd_addr_dev(cdnsp_get_slot_ctx(&pdev->out_ctx));
936 /* Zero the input context control for later use. */
937 ctrl_ctx->add_flags = 0;
938 ctrl_ctx->drop_flags = 0;
943 void cdnsp_set_usb2_hardware_lpm(struct cdnsp_device *pdev,
944 struct usb_request *req,
947 if (pdev->active_port != &pdev->usb2_port || !pdev->gadget.lpm_capable)
950 trace_cdnsp_lpm(enable);
953 writel(PORT_BESL(CDNSP_DEFAULT_BESL) | PORT_L1S_NYET | PORT_HLE,
954 &pdev->active_port->regs->portpmsc);
956 writel(PORT_L1S_NYET, &pdev->active_port->regs->portpmsc);
959 static int cdnsp_get_frame(struct cdnsp_device *pdev)
961 return readl(&pdev->run_regs->microframe_index) >> 3;
964 static int cdnsp_gadget_ep_enable(struct usb_ep *ep,
965 const struct usb_endpoint_descriptor *desc)
967 struct cdnsp_input_control_ctx *ctrl_ctx;
968 struct cdnsp_device *pdev;
969 struct cdnsp_ep *pep;
974 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT ||
975 !desc->wMaxPacketSize)
978 pep = to_cdnsp_ep(ep);
980 pep->ep_state &= ~EP_UNCONFIGURED;
982 if (dev_WARN_ONCE(pdev->dev, pep->ep_state & EP_ENABLED,
983 "%s is already enabled\n", pep->name))
986 spin_lock_irqsave(&pdev->lock, flags);
988 added_ctxs = cdnsp_get_endpoint_flag(desc);
989 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
990 dev_err(pdev->dev, "ERROR: Bad endpoint number\n");
995 pep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
997 if (pdev->gadget.speed == USB_SPEED_FULL) {
998 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT)
999 pep->interval = desc->bInterval << 3;
1000 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC)
1001 pep->interval = BIT(desc->bInterval - 1) << 3;
1004 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) {
1005 if (pep->interval > BIT(12)) {
1006 dev_err(pdev->dev, "bInterval %d not supported\n",
1011 cdnsp_set_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
1014 ret = cdnsp_endpoint_init(pdev, pep, GFP_ATOMIC);
1018 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
1019 ctrl_ctx->add_flags = cpu_to_le32(added_ctxs);
1020 ctrl_ctx->drop_flags = 0;
1022 ret = cdnsp_update_eps_configuration(pdev, pep);
1024 cdnsp_free_endpoint_rings(pdev, pep);
1028 pep->ep_state |= EP_ENABLED;
1029 pep->ep_state &= ~EP_STOPPED;
1032 trace_cdnsp_ep_enable_end(pep, 0);
1033 spin_unlock_irqrestore(&pdev->lock, flags);
1038 static int cdnsp_gadget_ep_disable(struct usb_ep *ep)
1040 struct cdnsp_input_control_ctx *ctrl_ctx;
1041 struct cdnsp_request *preq;
1042 struct cdnsp_device *pdev;
1043 struct cdnsp_ep *pep;
1044 unsigned long flags;
1051 pep = to_cdnsp_ep(ep);
1054 spin_lock_irqsave(&pdev->lock, flags);
1056 if (!(pep->ep_state & EP_ENABLED)) {
1057 dev_err(pdev->dev, "%s is already disabled\n", pep->name);
1062 pep->ep_state |= EP_DIS_IN_RROGRESS;
1064 /* Endpoint was unconfigured by Reset Device command. */
1065 if (!(pep->ep_state & EP_UNCONFIGURED))
1066 cdnsp_cmd_stop_ep(pdev, pep);
1068 /* Remove all queued USB requests. */
1069 while (!list_empty(&pep->pending_list)) {
1070 preq = next_request(&pep->pending_list);
1071 cdnsp_ep_dequeue(pep, preq);
1074 cdnsp_invalidate_ep_events(pdev, pep);
1076 pep->ep_state &= ~EP_DIS_IN_RROGRESS;
1077 drop_flag = cdnsp_get_endpoint_flag(pep->endpoint.desc);
1078 ctrl_ctx = cdnsp_get_input_control_ctx(&pdev->in_ctx);
1079 ctrl_ctx->drop_flags = cpu_to_le32(drop_flag);
1080 ctrl_ctx->add_flags = 0;
1082 cdnsp_endpoint_zero(pdev, pep);
1084 if (!(pep->ep_state & EP_UNCONFIGURED))
1085 ret = cdnsp_update_eps_configuration(pdev, pep);
1087 cdnsp_free_endpoint_rings(pdev, pep);
1089 pep->ep_state &= ~(EP_ENABLED | EP_UNCONFIGURED);
1090 pep->ep_state |= EP_STOPPED;
1093 trace_cdnsp_ep_disable_end(pep, 0);
1094 spin_unlock_irqrestore(&pdev->lock, flags);
1099 static struct usb_request *cdnsp_gadget_ep_alloc_request(struct usb_ep *ep,
1102 struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1103 struct cdnsp_request *preq;
1105 preq = kzalloc(sizeof(*preq), gfp_flags);
1109 preq->epnum = pep->number;
1112 trace_cdnsp_alloc_request(preq);
1114 return &preq->request;
1117 static void cdnsp_gadget_ep_free_request(struct usb_ep *ep,
1118 struct usb_request *request)
1120 struct cdnsp_request *preq = to_cdnsp_request(request);
1122 trace_cdnsp_free_request(preq);
1126 static int cdnsp_gadget_ep_queue(struct usb_ep *ep,
1127 struct usb_request *request,
1130 struct cdnsp_request *preq;
1131 struct cdnsp_device *pdev;
1132 struct cdnsp_ep *pep;
1133 unsigned long flags;
1136 if (!request || !ep)
1139 pep = to_cdnsp_ep(ep);
1142 if (!(pep->ep_state & EP_ENABLED)) {
1143 dev_err(pdev->dev, "%s: can't queue to disabled endpoint\n",
1148 preq = to_cdnsp_request(request);
1149 spin_lock_irqsave(&pdev->lock, flags);
1150 ret = cdnsp_ep_enqueue(pep, preq);
1151 spin_unlock_irqrestore(&pdev->lock, flags);
1156 static int cdnsp_gadget_ep_dequeue(struct usb_ep *ep,
1157 struct usb_request *request)
1159 struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1160 struct cdnsp_device *pdev = pep->pdev;
1161 unsigned long flags;
1164 if (request->status != -EINPROGRESS)
1167 if (!pep->endpoint.desc) {
1169 "%s: can't dequeue to disabled endpoint\n",
1174 /* Requests has been dequeued during disabling endpoint. */
1175 if (!(pep->ep_state & EP_ENABLED))
1178 spin_lock_irqsave(&pdev->lock, flags);
1179 ret = cdnsp_ep_dequeue(pep, to_cdnsp_request(request));
1180 spin_unlock_irqrestore(&pdev->lock, flags);
1185 static int cdnsp_gadget_ep_set_halt(struct usb_ep *ep, int value)
1187 struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1188 struct cdnsp_device *pdev = pep->pdev;
1189 struct cdnsp_request *preq;
1190 unsigned long flags;
1193 spin_lock_irqsave(&pdev->lock, flags);
1195 preq = next_request(&pep->pending_list);
1198 trace_cdnsp_ep_busy_try_halt_again(pep, 0);
1204 ret = cdnsp_halt_endpoint(pdev, pep, value);
1207 spin_unlock_irqrestore(&pdev->lock, flags);
1211 static int cdnsp_gadget_ep_set_wedge(struct usb_ep *ep)
1213 struct cdnsp_ep *pep = to_cdnsp_ep(ep);
1214 struct cdnsp_device *pdev = pep->pdev;
1215 unsigned long flags;
1218 spin_lock_irqsave(&pdev->lock, flags);
1219 pep->ep_state |= EP_WEDGE;
1220 ret = cdnsp_halt_endpoint(pdev, pep, 1);
1221 spin_unlock_irqrestore(&pdev->lock, flags);
1226 static const struct usb_ep_ops cdnsp_gadget_ep0_ops = {
1227 .enable = cdnsp_gadget_ep_enable,
1228 .disable = cdnsp_gadget_ep_disable,
1229 .alloc_request = cdnsp_gadget_ep_alloc_request,
1230 .free_request = cdnsp_gadget_ep_free_request,
1231 .queue = cdnsp_gadget_ep_queue,
1232 .dequeue = cdnsp_gadget_ep_dequeue,
1233 .set_halt = cdnsp_gadget_ep_set_halt,
1234 .set_wedge = cdnsp_gadget_ep_set_wedge,
1237 static const struct usb_ep_ops cdnsp_gadget_ep_ops = {
1238 .enable = cdnsp_gadget_ep_enable,
1239 .disable = cdnsp_gadget_ep_disable,
1240 .alloc_request = cdnsp_gadget_ep_alloc_request,
1241 .free_request = cdnsp_gadget_ep_free_request,
1242 .queue = cdnsp_gadget_ep_queue,
1243 .dequeue = cdnsp_gadget_ep_dequeue,
1244 .set_halt = cdnsp_gadget_ep_set_halt,
1245 .set_wedge = cdnsp_gadget_ep_set_wedge,
1248 void cdnsp_gadget_giveback(struct cdnsp_ep *pep,
1249 struct cdnsp_request *preq,
1252 struct cdnsp_device *pdev = pep->pdev;
1254 list_del(&preq->list);
1256 if (preq->request.status == -EINPROGRESS)
1257 preq->request.status = status;
1259 usb_gadget_unmap_request_by_dev(pdev->dev, &preq->request,
1262 trace_cdnsp_request_giveback(preq);
1264 if (preq != &pdev->ep0_preq) {
1265 spin_unlock(&pdev->lock);
1266 usb_gadget_giveback_request(&pep->endpoint, &preq->request);
1267 spin_lock(&pdev->lock);
1271 static struct usb_endpoint_descriptor cdnsp_gadget_ep0_desc = {
1272 .bLength = USB_DT_ENDPOINT_SIZE,
1273 .bDescriptorType = USB_DT_ENDPOINT,
1274 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1277 static int cdnsp_run(struct cdnsp_device *pdev,
1278 enum usb_device_speed speed)
1284 temp = readl(&pdev->ir_set->irq_control);
1285 temp &= ~IMOD_INTERVAL_MASK;
1286 temp |= ((IMOD_DEFAULT_INTERVAL / 250) & IMOD_INTERVAL_MASK);
1287 writel(temp, &pdev->ir_set->irq_control);
1289 temp = readl(&pdev->port3x_regs->mode_addr);
1292 case USB_SPEED_SUPER_PLUS:
1293 temp |= CFG_3XPORT_SSP_SUPPORT;
1295 case USB_SPEED_SUPER:
1296 temp &= ~CFG_3XPORT_SSP_SUPPORT;
1298 case USB_SPEED_HIGH:
1300 case USB_SPEED_FULL:
1301 fs_speed = PORT_REG6_FORCE_FS;
1304 dev_err(pdev->dev, "invalid maximum_speed parameter %d\n",
1307 case USB_SPEED_UNKNOWN:
1308 /* Default to superspeed. */
1309 speed = USB_SPEED_SUPER;
1313 if (speed >= USB_SPEED_SUPER) {
1314 writel(temp, &pdev->port3x_regs->mode_addr);
1315 cdnsp_set_link_state(pdev, &pdev->usb3_port.regs->portsc,
1318 cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
1321 cdnsp_set_link_state(pdev, &pdev->usb2_port.regs->portsc,
1324 cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1326 writel(PORT_REG6_L1_L0_HW_EN | fs_speed, &pdev->port20_regs->port_reg6);
1328 ret = cdnsp_start(pdev);
1334 temp = readl(&pdev->op_regs->command);
1336 writel(temp, &pdev->op_regs->command);
1338 temp = readl(&pdev->ir_set->irq_pending);
1339 writel(IMAN_IE_SET(temp), &pdev->ir_set->irq_pending);
1341 trace_cdnsp_init("Controller ready to work");
1348 static int cdnsp_gadget_udc_start(struct usb_gadget *g,
1349 struct usb_gadget_driver *driver)
1351 enum usb_device_speed max_speed = driver->max_speed;
1352 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1353 unsigned long flags;
1356 spin_lock_irqsave(&pdev->lock, flags);
1357 pdev->gadget_driver = driver;
1359 /* limit speed if necessary */
1360 max_speed = min(driver->max_speed, g->max_speed);
1361 ret = cdnsp_run(pdev, max_speed);
1363 spin_unlock_irqrestore(&pdev->lock, flags);
1369 * Update Event Ring Dequeue Pointer:
1370 * - When all events have finished
1371 * - To avoid "Event Ring Full Error" condition
1373 void cdnsp_update_erst_dequeue(struct cdnsp_device *pdev,
1374 union cdnsp_trb *event_ring_deq,
1380 temp_64 = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
1382 /* If necessary, update the HW's version of the event ring deq ptr. */
1383 if (event_ring_deq != pdev->event_ring->dequeue) {
1384 deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
1385 pdev->event_ring->dequeue);
1386 temp_64 &= ERST_PTR_MASK;
1387 temp_64 |= ((u64)deq & (u64)~ERST_PTR_MASK);
1390 /* Clear the event handler busy flag (RW1C). */
1392 temp_64 |= ERST_EHB;
1394 temp_64 &= ~ERST_EHB;
1396 cdnsp_write_64(temp_64, &pdev->ir_set->erst_dequeue);
1399 static void cdnsp_clear_cmd_ring(struct cdnsp_device *pdev)
1401 struct cdnsp_segment *seg;
1405 cdnsp_initialize_ring_info(pdev->cmd_ring);
1407 seg = pdev->cmd_ring->first_seg;
1408 for (i = 0; i < pdev->cmd_ring->num_segs; i++) {
1409 memset(seg->trbs, 0,
1410 sizeof(union cdnsp_trb) * (TRBS_PER_SEGMENT - 1));
1414 /* Set the address in the Command Ring Control register. */
1415 val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring);
1416 val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) |
1417 (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) |
1418 pdev->cmd_ring->cycle_state;
1419 cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring);
1422 static void cdnsp_consume_all_events(struct cdnsp_device *pdev)
1424 struct cdnsp_segment *event_deq_seg;
1425 union cdnsp_trb *event_ring_deq;
1426 union cdnsp_trb *event;
1429 event_ring_deq = pdev->event_ring->dequeue;
1430 event_deq_seg = pdev->event_ring->deq_seg;
1431 event = pdev->event_ring->dequeue;
1433 /* Update ring dequeue pointer. */
1435 cycle_bit = (le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE);
1437 /* Does the controller or driver own the TRB? */
1438 if (cycle_bit != pdev->event_ring->cycle_state)
1441 cdnsp_inc_deq(pdev, pdev->event_ring);
1443 if (!cdnsp_last_trb_on_seg(event_deq_seg, event)) {
1448 if (cdnsp_last_trb_on_ring(pdev->event_ring, event_deq_seg,
1452 event_deq_seg = event_deq_seg->next;
1453 event = event_deq_seg->trbs;
1456 cdnsp_update_erst_dequeue(pdev, event_ring_deq, 1);
1459 static void cdnsp_stop(struct cdnsp_device *pdev)
1463 /* Remove internally queued request for ep0. */
1464 if (!list_empty(&pdev->eps[0].pending_list)) {
1465 struct cdnsp_request *req;
1467 req = next_request(&pdev->eps[0].pending_list);
1468 if (req == &pdev->ep0_preq)
1469 cdnsp_ep_dequeue(&pdev->eps[0], req);
1472 cdnsp_disable_port(pdev, &pdev->usb2_port.regs->portsc);
1473 cdnsp_disable_port(pdev, &pdev->usb3_port.regs->portsc);
1474 cdnsp_disable_slot(pdev);
1477 temp = readl(&pdev->op_regs->status);
1478 writel((temp & ~0x1fff) | STS_EINT, &pdev->op_regs->status);
1479 temp = readl(&pdev->ir_set->irq_pending);
1480 writel(IMAN_IE_CLEAR(temp), &pdev->ir_set->irq_pending);
1482 cdnsp_clear_port_change_bit(pdev, &pdev->usb2_port.regs->portsc);
1483 cdnsp_clear_port_change_bit(pdev, &pdev->usb3_port.regs->portsc);
1485 /* Clear interrupt line */
1486 temp = readl(&pdev->ir_set->irq_pending);
1488 writel(temp, &pdev->ir_set->irq_pending);
1490 cdnsp_consume_all_events(pdev);
1491 cdnsp_clear_cmd_ring(pdev);
1493 trace_cdnsp_exit("Controller stopped.");
1498 * This function is called by the gadget core when the driver is removed.
1499 * Disable slot, disable IRQs, and quiesce the controller.
1501 static int cdnsp_gadget_udc_stop(struct usb_gadget *g)
1503 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1504 unsigned long flags;
1506 spin_lock_irqsave(&pdev->lock, flags);
1508 pdev->gadget_driver = NULL;
1509 spin_unlock_irqrestore(&pdev->lock, flags);
1514 static int cdnsp_gadget_get_frame(struct usb_gadget *g)
1516 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1518 return cdnsp_get_frame(pdev);
1521 static void __cdnsp_gadget_wakeup(struct cdnsp_device *pdev)
1523 struct cdnsp_port_regs __iomem *port_regs;
1526 port_regs = pdev->active_port->regs;
1527 portsc = readl(&port_regs->portsc) & PORT_PLS_MASK;
1529 /* Remote wakeup feature is not enabled by host. */
1530 if (pdev->gadget.speed < USB_SPEED_SUPER && portsc == XDEV_U2) {
1531 portpm = readl(&port_regs->portpmsc);
1533 if (!(portpm & PORT_RWE))
1537 if (portsc == XDEV_U3 && !pdev->may_wakeup)
1540 cdnsp_set_link_state(pdev, &port_regs->portsc, XDEV_U0);
1542 pdev->cdnsp_state |= CDNSP_WAKEUP_PENDING;
1545 static int cdnsp_gadget_wakeup(struct usb_gadget *g)
1547 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1548 unsigned long flags;
1550 spin_lock_irqsave(&pdev->lock, flags);
1551 __cdnsp_gadget_wakeup(pdev);
1552 spin_unlock_irqrestore(&pdev->lock, flags);
1557 static int cdnsp_gadget_set_selfpowered(struct usb_gadget *g,
1560 struct cdnsp_device *pdev = gadget_to_cdnsp(g);
1561 unsigned long flags;
1563 spin_lock_irqsave(&pdev->lock, flags);
1564 g->is_selfpowered = !!is_selfpowered;
1565 spin_unlock_irqrestore(&pdev->lock, flags);
1570 static int cdnsp_gadget_pullup(struct usb_gadget *gadget, int is_on)
1572 struct cdnsp_device *pdev = gadget_to_cdnsp(gadget);
1573 struct cdns *cdns = dev_get_drvdata(pdev->dev);
1574 unsigned long flags;
1576 trace_cdnsp_pullup(is_on);
1579 * Disable events handling while controller is being
1582 disable_irq(cdns->dev_irq);
1583 spin_lock_irqsave(&pdev->lock, flags);
1586 cdnsp_reset_device(pdev);
1587 cdns_clear_vbus(cdns);
1589 cdns_set_vbus(cdns);
1592 spin_unlock_irqrestore(&pdev->lock, flags);
1593 enable_irq(cdns->dev_irq);
1598 static const struct usb_gadget_ops cdnsp_gadget_ops = {
1599 .get_frame = cdnsp_gadget_get_frame,
1600 .wakeup = cdnsp_gadget_wakeup,
1601 .set_selfpowered = cdnsp_gadget_set_selfpowered,
1602 .pullup = cdnsp_gadget_pullup,
1603 .udc_start = cdnsp_gadget_udc_start,
1604 .udc_stop = cdnsp_gadget_udc_stop,
1607 static void cdnsp_get_ep_buffering(struct cdnsp_device *pdev,
1608 struct cdnsp_ep *pep)
1610 void __iomem *reg = &pdev->cap_regs->hc_capbase;
1613 reg += cdnsp_find_next_ext_cap(reg, 0, XBUF_CAP_ID);
1615 if (!pep->direction) {
1616 pep->buffering = readl(reg + XBUF_RX_TAG_MASK_0_OFFSET);
1617 pep->buffering_period = readl(reg + XBUF_RX_TAG_MASK_1_OFFSET);
1618 pep->buffering = (pep->buffering + 1) / 2;
1619 pep->buffering_period = (pep->buffering_period + 1) / 2;
1623 endpoints = HCS_ENDPOINTS(pdev->hcs_params1) / 2;
1625 /* Set to XBUF_TX_TAG_MASK_0 register. */
1626 reg += XBUF_TX_CMD_OFFSET + (endpoints * 2 + 2) * sizeof(u32);
1627 /* Set reg to XBUF_TX_TAG_MASK_N related with this endpoint. */
1628 reg += pep->number * sizeof(u32) * 2;
1630 pep->buffering = (readl(reg) + 1) / 2;
1631 pep->buffering_period = pep->buffering;
1634 static int cdnsp_gadget_init_endpoints(struct cdnsp_device *pdev)
1636 int max_streams = HCC_MAX_PSA(pdev->hcc_params);
1637 struct cdnsp_ep *pep;
1640 INIT_LIST_HEAD(&pdev->gadget.ep_list);
1642 if (max_streams < STREAM_LOG_STREAMS) {
1643 dev_err(pdev->dev, "Stream size %d not supported\n",
1648 max_streams = STREAM_LOG_STREAMS;
1650 for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
1651 bool direction = !(i & 1); /* Start from OUT endpoint. */
1652 u8 epnum = ((i + 1) >> 1);
1654 if (!CDNSP_IF_EP_EXIST(pdev, epnum, direction))
1657 pep = &pdev->eps[i];
1659 pep->number = epnum;
1660 pep->direction = direction; /* 0 for OUT, 1 for IN. */
1663 * Ep0 is bidirectional, so ep0in and ep0out are represented by
1667 snprintf(pep->name, sizeof(pep->name), "ep%d%s",
1671 usb_ep_set_maxpacket_limit(&pep->endpoint, 512);
1672 pep->endpoint.maxburst = 1;
1673 pep->endpoint.ops = &cdnsp_gadget_ep0_ops;
1674 pep->endpoint.desc = &cdnsp_gadget_ep0_desc;
1675 pep->endpoint.comp_desc = NULL;
1676 pep->endpoint.caps.type_control = true;
1677 pep->endpoint.caps.dir_in = true;
1678 pep->endpoint.caps.dir_out = true;
1680 pdev->ep0_preq.epnum = pep->number;
1681 pdev->ep0_preq.pep = pep;
1682 pdev->gadget.ep0 = &pep->endpoint;
1684 snprintf(pep->name, sizeof(pep->name), "ep%d%s",
1685 epnum, (pep->direction) ? "in" : "out");
1687 pep->idx = (epnum * 2 + (direction ? 1 : 0)) - 1;
1688 usb_ep_set_maxpacket_limit(&pep->endpoint, 1024);
1690 pep->endpoint.max_streams = max_streams;
1691 pep->endpoint.ops = &cdnsp_gadget_ep_ops;
1692 list_add_tail(&pep->endpoint.ep_list,
1693 &pdev->gadget.ep_list);
1695 pep->endpoint.caps.type_iso = true;
1696 pep->endpoint.caps.type_bulk = true;
1697 pep->endpoint.caps.type_int = true;
1699 pep->endpoint.caps.dir_in = direction;
1700 pep->endpoint.caps.dir_out = !direction;
1703 pep->endpoint.name = pep->name;
1704 pep->in_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, pep->idx);
1705 pep->out_ctx = cdnsp_get_ep_ctx(&pdev->out_ctx, pep->idx);
1706 cdnsp_get_ep_buffering(pdev, pep);
1708 dev_dbg(pdev->dev, "Init %s, MPS: %04x SupType: "
1709 "CTRL: %s, INT: %s, BULK: %s, ISOC %s, "
1710 "SupDir IN: %s, OUT: %s\n",
1712 str_yes_no(pep->endpoint.caps.type_control),
1713 str_yes_no(pep->endpoint.caps.type_int),
1714 str_yes_no(pep->endpoint.caps.type_bulk),
1715 str_yes_no(pep->endpoint.caps.type_iso),
1716 str_yes_no(pep->endpoint.caps.dir_in),
1717 str_yes_no(pep->endpoint.caps.dir_out));
1719 INIT_LIST_HEAD(&pep->pending_list);
1725 static void cdnsp_gadget_free_endpoints(struct cdnsp_device *pdev)
1727 struct cdnsp_ep *pep;
1730 for (i = 0; i < CDNSP_ENDPOINTS_NUM; i++) {
1731 pep = &pdev->eps[i];
1732 if (pep->number != 0 && pep->out_ctx)
1733 list_del(&pep->endpoint.ep_list);
1737 void cdnsp_disconnect_gadget(struct cdnsp_device *pdev)
1739 pdev->cdnsp_state |= CDNSP_STATE_DISCONNECT_PENDING;
1741 if (pdev->gadget_driver && pdev->gadget_driver->disconnect) {
1742 spin_unlock(&pdev->lock);
1743 pdev->gadget_driver->disconnect(&pdev->gadget);
1744 spin_lock(&pdev->lock);
1747 pdev->gadget.speed = USB_SPEED_UNKNOWN;
1748 usb_gadget_set_state(&pdev->gadget, USB_STATE_NOTATTACHED);
1750 pdev->cdnsp_state &= ~CDNSP_STATE_DISCONNECT_PENDING;
1753 void cdnsp_suspend_gadget(struct cdnsp_device *pdev)
1755 if (pdev->gadget_driver && pdev->gadget_driver->suspend) {
1756 spin_unlock(&pdev->lock);
1757 pdev->gadget_driver->suspend(&pdev->gadget);
1758 spin_lock(&pdev->lock);
1762 void cdnsp_resume_gadget(struct cdnsp_device *pdev)
1764 if (pdev->gadget_driver && pdev->gadget_driver->resume) {
1765 spin_unlock(&pdev->lock);
1766 pdev->gadget_driver->resume(&pdev->gadget);
1767 spin_lock(&pdev->lock);
1771 void cdnsp_irq_reset(struct cdnsp_device *pdev)
1773 struct cdnsp_port_regs __iomem *port_regs;
1775 cdnsp_reset_device(pdev);
1777 port_regs = pdev->active_port->regs;
1778 pdev->gadget.speed = cdnsp_port_speed(readl(port_regs));
1780 spin_unlock(&pdev->lock);
1781 usb_gadget_udc_reset(&pdev->gadget, pdev->gadget_driver);
1782 spin_lock(&pdev->lock);
1784 switch (pdev->gadget.speed) {
1785 case USB_SPEED_SUPER_PLUS:
1786 case USB_SPEED_SUPER:
1787 cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1788 pdev->gadget.ep0->maxpacket = 512;
1790 case USB_SPEED_HIGH:
1791 case USB_SPEED_FULL:
1792 cdnsp_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1793 pdev->gadget.ep0->maxpacket = 64;
1796 /* Low speed is not supported. */
1797 dev_err(pdev->dev, "Unknown device speed\n");
1801 cdnsp_clear_chicken_bits_2(pdev, CHICKEN_XDMA_2_TP_CACHE_DIS);
1802 cdnsp_setup_device(pdev, SETUP_CONTEXT_ONLY);
1803 usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
1806 static void cdnsp_get_rev_cap(struct cdnsp_device *pdev)
1808 void __iomem *reg = &pdev->cap_regs->hc_capbase;
1810 reg += cdnsp_find_next_ext_cap(reg, 0, RTL_REV_CAP);
1811 pdev->rev_cap = reg;
1813 pdev->rtl_revision = readl(&pdev->rev_cap->rtl_revision);
1815 dev_info(pdev->dev, "Rev: %08x/%08x, eps: %08x, buff: %08x/%08x\n",
1816 readl(&pdev->rev_cap->ctrl_revision),
1817 readl(&pdev->rev_cap->rtl_revision),
1818 readl(&pdev->rev_cap->ep_supported),
1819 readl(&pdev->rev_cap->rx_buff_size),
1820 readl(&pdev->rev_cap->tx_buff_size));
1823 static int cdnsp_gen_setup(struct cdnsp_device *pdev)
1828 pdev->cap_regs = pdev->regs;
1829 pdev->op_regs = pdev->regs +
1830 HC_LENGTH(readl(&pdev->cap_regs->hc_capbase));
1831 pdev->run_regs = pdev->regs +
1832 (readl(&pdev->cap_regs->run_regs_off) & RTSOFF_MASK);
1834 /* Cache read-only capability registers */
1835 pdev->hcs_params1 = readl(&pdev->cap_regs->hcs_params1);
1836 pdev->hcc_params = readl(&pdev->cap_regs->hc_capbase);
1837 pdev->hci_version = HC_VERSION(pdev->hcc_params);
1838 pdev->hcc_params = readl(&pdev->cap_regs->hcc_params);
1841 * Override the APB timeout value to give the controller more time for
1842 * enabling UTMI clock and synchronizing APB and UTMI clock domains.
1843 * This fix is platform specific and is required to fixes issue with
1844 * reading incorrect value from PORTSC register after resuming
1847 cdnsp_set_apb_timeout_value(pdev);
1849 cdnsp_get_rev_cap(pdev);
1851 /* Make sure the Device Controller is halted. */
1852 ret = cdnsp_halt(pdev);
1856 /* Reset the internal controller memory state and registers. */
1857 ret = cdnsp_reset(pdev);
1862 * Set dma_mask and coherent_dma_mask to 64-bits,
1863 * if controller supports 64-bit addressing.
1865 if (HCC_64BIT_ADDR(pdev->hcc_params) &&
1866 !dma_set_mask(pdev->dev, DMA_BIT_MASK(64))) {
1867 dev_dbg(pdev->dev, "Enabling 64-bit DMA addresses.\n");
1868 dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(64));
1871 * This is to avoid error in cases where a 32-bit USB
1872 * controller is used on a 64-bit capable system.
1874 ret = dma_set_mask(pdev->dev, DMA_BIT_MASK(32));
1878 dev_dbg(pdev->dev, "Enabling 32-bit DMA addresses.\n");
1879 dma_set_coherent_mask(pdev->dev, DMA_BIT_MASK(32));
1882 spin_lock_init(&pdev->lock);
1884 ret = cdnsp_mem_init(pdev);
1889 * Software workaround for U1: after transition
1890 * to U1 the controller starts gating clock, and in some cases,
1891 * it causes that controller stack.
1893 reg = readl(&pdev->port3x_regs->mode_2);
1894 reg &= ~CFG_3XPORT_U1_PIPE_CLK_GATE_EN;
1895 writel(reg, &pdev->port3x_regs->mode_2);
1900 static int __cdnsp_gadget_init(struct cdns *cdns)
1902 struct cdnsp_device *pdev;
1906 cdns_drd_gadget_on(cdns);
1908 pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
1912 pm_runtime_get_sync(cdns->dev);
1914 cdns->gadget_dev = pdev;
1915 pdev->dev = cdns->dev;
1916 pdev->regs = cdns->dev_regs;
1917 max_speed = usb_get_maximum_speed(cdns->dev);
1919 switch (max_speed) {
1920 case USB_SPEED_FULL:
1921 case USB_SPEED_HIGH:
1922 case USB_SPEED_SUPER:
1923 case USB_SPEED_SUPER_PLUS:
1926 dev_err(cdns->dev, "invalid speed parameter %d\n", max_speed);
1928 case USB_SPEED_UNKNOWN:
1929 /* Default to SSP */
1930 max_speed = USB_SPEED_SUPER_PLUS;
1934 pdev->gadget.ops = &cdnsp_gadget_ops;
1935 pdev->gadget.name = "cdnsp-gadget";
1936 pdev->gadget.speed = USB_SPEED_UNKNOWN;
1937 pdev->gadget.sg_supported = 1;
1938 pdev->gadget.max_speed = max_speed;
1939 pdev->gadget.lpm_capable = 1;
1941 pdev->setup_buf = kzalloc(CDNSP_EP0_SETUP_SIZE, GFP_KERNEL);
1942 if (!pdev->setup_buf)
1946 * Controller supports not aligned buffer but it should improve
1949 pdev->gadget.quirk_ep_out_aligned_size = true;
1951 ret = cdnsp_gen_setup(pdev);
1953 dev_err(pdev->dev, "Generic initialization failed %d\n", ret);
1957 ret = cdnsp_gadget_init_endpoints(pdev);
1959 dev_err(pdev->dev, "failed to initialize endpoints\n");
1963 ret = usb_add_gadget_udc(pdev->dev, &pdev->gadget);
1965 dev_err(pdev->dev, "failed to register udc\n");
1966 goto free_endpoints;
1969 ret = devm_request_threaded_irq(pdev->dev, cdns->dev_irq,
1971 cdnsp_thread_irq_handler, IRQF_SHARED,
1972 dev_name(pdev->dev), pdev);
1979 usb_del_gadget_udc(&pdev->gadget);
1981 cdnsp_gadget_free_endpoints(pdev);
1985 cdnsp_mem_cleanup(pdev);
1987 kfree(pdev->setup_buf);
1994 static void cdnsp_gadget_exit(struct cdns *cdns)
1996 struct cdnsp_device *pdev = cdns->gadget_dev;
1998 devm_free_irq(pdev->dev, cdns->dev_irq, pdev);
1999 pm_runtime_mark_last_busy(cdns->dev);
2000 pm_runtime_put_autosuspend(cdns->dev);
2001 usb_del_gadget_udc(&pdev->gadget);
2002 cdnsp_gadget_free_endpoints(pdev);
2003 cdnsp_mem_cleanup(pdev);
2005 cdns->gadget_dev = NULL;
2006 cdns_drd_gadget_off(cdns);
2009 static int cdnsp_gadget_suspend(struct cdns *cdns, bool do_wakeup)
2011 struct cdnsp_device *pdev = cdns->gadget_dev;
2012 unsigned long flags;
2014 if (pdev->link_state == XDEV_U3)
2017 spin_lock_irqsave(&pdev->lock, flags);
2018 cdnsp_disconnect_gadget(pdev);
2020 spin_unlock_irqrestore(&pdev->lock, flags);
2025 static int cdnsp_gadget_resume(struct cdns *cdns, bool lost_power)
2027 struct cdnsp_device *pdev = cdns->gadget_dev;
2028 enum usb_device_speed max_speed;
2029 unsigned long flags;
2032 if (!pdev->gadget_driver)
2035 spin_lock_irqsave(&pdev->lock, flags);
2036 max_speed = pdev->gadget_driver->max_speed;
2038 /* Limit speed if necessary. */
2039 max_speed = min(max_speed, pdev->gadget.max_speed);
2041 ret = cdnsp_run(pdev, max_speed);
2043 if (pdev->link_state == XDEV_U3)
2044 __cdnsp_gadget_wakeup(pdev);
2046 spin_unlock_irqrestore(&pdev->lock, flags);
2052 * cdnsp_gadget_init - initialize device structure
2053 * @cdns: cdnsp instance
2055 * This function initializes the gadget.
2057 int cdnsp_gadget_init(struct cdns *cdns)
2059 struct cdns_role_driver *rdrv;
2061 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
2065 rdrv->start = __cdnsp_gadget_init;
2066 rdrv->stop = cdnsp_gadget_exit;
2067 rdrv->suspend = cdnsp_gadget_suspend;
2068 rdrv->resume = cdnsp_gadget_resume;
2069 rdrv->state = CDNS_ROLE_STATE_INACTIVE;
2070 rdrv->name = "gadget";
2071 cdns->roles[USB_ROLE_DEVICE] = rdrv;