1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence USBSS DRD Driver - gadget side.
5 * Copyright (C) 2018 Cadence Design Systems.
6 * Copyright (C) 2017-2018 NXP
8 * Authors: Pawel Jez <pjez@cadence.com>,
9 * Pawel Laszczak <pawell@cadence.com>
10 * Peter Chen <peter.chen@nxp.com>
15 #include <dm/device_compat.h>
16 #include <asm/cache.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/usb/composite.h>
20 #include <linux/iopoll.h>
25 #define readl_poll_timeout_atomic readl_poll_timeout
26 #define usleep_range(a, b) udelay((b))
28 static struct usb_endpoint_descriptor cdns3_gadget_ep0_desc = {
29 .bLength = USB_DT_ENDPOINT_SIZE,
30 .bDescriptorType = USB_DT_ENDPOINT,
31 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
35 * cdns3_ep0_run_transfer - Do transfer on default endpoint hardware
36 * @priv_dev: extended gadget object
37 * @dma_addr: physical address where data is/will be stored
38 * @length: data length
39 * @erdy: set it to 1 when ERDY packet should be sent -
40 * exit from flow control state
42 static void cdns3_ep0_run_transfer(struct cdns3_device *priv_dev,
44 unsigned int length, int erdy, int zlp)
46 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
47 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
49 priv_ep->trb_pool[0].buffer = TRB_BUFFER(dma_addr);
50 priv_ep->trb_pool[0].length = TRB_LEN(length);
53 priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_TYPE(TRB_NORMAL);
54 priv_ep->trb_pool[1].buffer = TRB_BUFFER(dma_addr);
55 priv_ep->trb_pool[1].length = TRB_LEN(0);
56 priv_ep->trb_pool[1].control = TRB_CYCLE | TRB_IOC |
59 priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_IOC |
61 priv_ep->trb_pool[1].control = 0;
65 flush_dcache_range((unsigned long)priv_ep->trb_pool,
66 (unsigned long)priv_ep->trb_pool +
67 ROUND(sizeof(struct cdns3_trb) * 2,
68 CONFIG_SYS_CACHELINE_SIZE));
70 trace_cdns3_prepare_trb(priv_ep, priv_ep->trb_pool);
72 cdns3_select_ep(priv_dev, priv_dev->ep0_data_dir);
74 writel(EP_STS_TRBERR, ®s->ep_sts);
75 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma), ®s->ep_traddr);
76 trace_cdns3_doorbell_ep0(priv_dev->ep0_data_dir ? "ep0in" : "ep0out",
77 readl(®s->ep_traddr));
79 /* TRB should be prepared before starting transfer. */
80 writel(EP_CMD_DRDY, ®s->ep_cmd);
82 /* Resume controller before arming transfer. */
83 __cdns3_gadget_wakeup(priv_dev);
86 writel(EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
90 * cdns3_ep0_delegate_req - Returns status of handling setup packet
91 * Setup is handled by gadget driver
92 * @priv_dev: extended gadget object
93 * @ctrl_req: pointer to received setup packet
95 * Returns zero on success or negative value on failure
97 static int cdns3_ep0_delegate_req(struct cdns3_device *priv_dev,
98 struct usb_ctrlrequest *ctrl_req)
102 spin_unlock(&priv_dev->lock);
103 priv_dev->setup_pending = 1;
104 ret = priv_dev->gadget_driver->setup(&priv_dev->gadget, ctrl_req);
105 priv_dev->setup_pending = 0;
106 spin_lock(&priv_dev->lock);
110 static void cdns3_prepare_setup_packet(struct cdns3_device *priv_dev)
112 priv_dev->ep0_data_dir = 0;
113 priv_dev->ep0_stage = CDNS3_SETUP_STAGE;
114 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma,
115 sizeof(struct usb_ctrlrequest), 0, 0);
118 static void cdns3_ep0_complete_setup(struct cdns3_device *priv_dev,
119 u8 send_stall, u8 send_erdy)
121 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
122 struct usb_request *request;
124 request = cdns3_next_request(&priv_ep->pending_req_list);
126 list_del_init(&request->list);
129 trace_cdns3_halt(priv_ep, send_stall, 0);
130 /* set_stall on ep0 */
131 cdns3_select_ep(priv_dev, 0x00);
132 writel(EP_CMD_SSTALL, &priv_dev->regs->ep_cmd);
134 cdns3_prepare_setup_packet(priv_dev);
137 priv_dev->ep0_stage = CDNS3_SETUP_STAGE;
138 writel((send_erdy ? EP_CMD_ERDY : 0) | EP_CMD_REQ_CMPL,
139 &priv_dev->regs->ep_cmd);
141 cdns3_allow_enable_l1(priv_dev, 1);
145 * cdns3_req_ep0_set_configuration - Handling of SET_CONFIG standard USB request
146 * @priv_dev: extended gadget object
147 * @ctrl_req: pointer to received setup packet
149 * Returns 0 if success, USB_GADGET_DELAYED_STATUS on deferred status stage,
150 * error code on error
152 static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev,
153 struct usb_ctrlrequest *ctrl_req)
155 enum usb_device_state device_state = priv_dev->gadget.state;
156 struct cdns3_endpoint *priv_ep;
157 u32 config = le16_to_cpu(ctrl_req->wValue);
161 switch (device_state) {
162 case USB_STATE_ADDRESS:
163 /* Configure non-control EPs */
164 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
165 priv_ep = priv_dev->eps[i];
169 if (priv_ep->flags & EP_CLAIMED)
170 cdns3_ep_config(priv_ep);
173 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
179 cdns3_set_hw_configuration(priv_dev);
181 cdns3_hw_reset_eps_config(priv_dev);
182 usb_gadget_set_state(&priv_dev->gadget,
186 case USB_STATE_CONFIGURED:
187 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
189 if (!config && !result) {
190 cdns3_hw_reset_eps_config(priv_dev);
191 usb_gadget_set_state(&priv_dev->gadget,
203 * cdns3_req_ep0_set_address - Handling of SET_ADDRESS standard USB request
204 * @priv_dev: extended gadget object
205 * @ctrl_req: pointer to received setup packet
207 * Returns 0 if success, error code on error
209 static int cdns3_req_ep0_set_address(struct cdns3_device *priv_dev,
210 struct usb_ctrlrequest *ctrl_req)
212 enum usb_device_state device_state = priv_dev->gadget.state;
216 addr = le16_to_cpu(ctrl_req->wValue);
218 if (addr > USB_DEVICE_MAX_ADDRESS) {
219 dev_err(priv_dev->dev,
220 "Device address (%d) cannot be greater than %d\n",
221 addr, USB_DEVICE_MAX_ADDRESS);
225 if (device_state == USB_STATE_CONFIGURED) {
226 dev_err(priv_dev->dev,
227 "can't set_address from configured state\n");
231 reg = readl(&priv_dev->regs->usb_cmd);
233 writel(reg | USB_CMD_FADDR(addr) | USB_CMD_SET_ADDR,
234 &priv_dev->regs->usb_cmd);
236 usb_gadget_set_state(&priv_dev->gadget,
237 (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT));
243 * cdns3_req_ep0_get_status - Handling of GET_STATUS standard USB request
244 * @priv_dev: extended gadget object
245 * @ctrl_req: pointer to received setup packet
247 * Returns 0 if success, error code on error
249 static int cdns3_req_ep0_get_status(struct cdns3_device *priv_dev,
250 struct usb_ctrlrequest *ctrl)
252 __le16 *response_pkt;
256 recip = ctrl->bRequestType & USB_RECIP_MASK;
259 case USB_RECIP_DEVICE:
261 if (priv_dev->is_selfpowered)
262 usb_status = BIT(USB_DEVICE_SELF_POWERED);
264 if (priv_dev->wake_up_flag)
265 usb_status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
267 if (priv_dev->gadget.speed != USB_SPEED_SUPER)
270 if (priv_dev->u1_allowed)
271 usb_status |= BIT(USB_DEV_STAT_U1_ENABLED);
273 if (priv_dev->u2_allowed)
274 usb_status |= BIT(USB_DEV_STAT_U2_ENABLED);
277 case USB_RECIP_INTERFACE:
278 return cdns3_ep0_delegate_req(priv_dev, ctrl);
279 case USB_RECIP_ENDPOINT:
280 /* check if endpoint is stalled */
281 cdns3_select_ep(priv_dev, ctrl->wIndex);
282 if (EP_STS_STALL(readl(&priv_dev->regs->ep_sts)))
283 usb_status = BIT(USB_ENDPOINT_HALT);
289 response_pkt = (__le16 *)priv_dev->setup_buf;
290 *response_pkt = cpu_to_le16(usb_status);
292 /* Flush setup response */
293 flush_dcache_range((unsigned long)priv_dev->setup_buf,
294 (unsigned long)priv_dev->setup_buf +
295 ROUND(sizeof(struct usb_ctrlrequest),
296 CONFIG_SYS_CACHELINE_SIZE));
298 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma,
299 sizeof(*response_pkt), 1, 0);
303 static int cdns3_ep0_feature_handle_device(struct cdns3_device *priv_dev,
304 struct usb_ctrlrequest *ctrl,
307 enum usb_device_state state;
308 enum usb_device_speed speed;
312 state = priv_dev->gadget.state;
313 speed = priv_dev->gadget.speed;
315 switch (ctrl->wValue) {
316 case USB_DEVICE_REMOTE_WAKEUP:
317 priv_dev->wake_up_flag = !!set;
319 case USB_DEVICE_U1_ENABLE:
320 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER)
323 priv_dev->u1_allowed = !!set;
325 case USB_DEVICE_U2_ENABLE:
326 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER)
329 priv_dev->u2_allowed = !!set;
331 case USB_DEVICE_LTM_ENABLE:
334 case USB_DEVICE_TEST_MODE:
335 if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
338 tmode = le16_to_cpu(ctrl->wIndex);
340 if (!set || (tmode & 0xff) != 0)
343 switch (tmode >> 8) {
348 cdns3_ep0_complete_setup(priv_dev, 0, 1);
350 * Little delay to give the controller some time
351 * for sending status stage.
352 * This time should be less then 3ms.
354 usleep_range(1000, 2000);
355 cdns3_set_register_bit(&priv_dev->regs->usb_cmd,
357 USB_STS_TMODE_SEL(tmode - 1));
370 static int cdns3_ep0_feature_handle_intf(struct cdns3_device *priv_dev,
371 struct usb_ctrlrequest *ctrl,
377 wValue = le16_to_cpu(ctrl->wValue);
380 case USB_INTRF_FUNC_SUSPEND:
389 static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev,
390 struct usb_ctrlrequest *ctrl,
393 struct cdns3_endpoint *priv_ep;
397 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
400 if (!(ctrl->wIndex & ~USB_DIR_IN))
403 index = cdns3_ep_addr_to_index(ctrl->wIndex);
404 priv_ep = priv_dev->eps[index];
406 cdns3_select_ep(priv_dev, ctrl->wIndex);
409 __cdns3_gadget_ep_set_halt(priv_ep);
410 else if (!(priv_ep->flags & EP_WEDGE))
411 ret = __cdns3_gadget_ep_clear_halt(priv_ep);
413 cdns3_select_ep(priv_dev, 0x00);
419 * cdns3_req_ep0_handle_feature -
420 * Handling of GET/SET_FEATURE standard USB request
422 * @priv_dev: extended gadget object
423 * @ctrl_req: pointer to received setup packet
424 * @set: must be set to 1 for SET_FEATURE request
426 * Returns 0 if success, error code on error
428 static int cdns3_req_ep0_handle_feature(struct cdns3_device *priv_dev,
429 struct usb_ctrlrequest *ctrl,
435 recip = ctrl->bRequestType & USB_RECIP_MASK;
438 case USB_RECIP_DEVICE:
439 ret = cdns3_ep0_feature_handle_device(priv_dev, ctrl, set);
441 case USB_RECIP_INTERFACE:
442 ret = cdns3_ep0_feature_handle_intf(priv_dev, ctrl, set);
444 case USB_RECIP_ENDPOINT:
445 ret = cdns3_ep0_feature_handle_endpoint(priv_dev, ctrl, set);
455 * cdns3_req_ep0_set_sel - Handling of SET_SEL standard USB request
456 * @priv_dev: extended gadget object
457 * @ctrl_req: pointer to received setup packet
459 * Returns 0 if success, error code on error
461 static int cdns3_req_ep0_set_sel(struct cdns3_device *priv_dev,
462 struct usb_ctrlrequest *ctrl_req)
464 if (priv_dev->gadget.state < USB_STATE_ADDRESS)
467 if (ctrl_req->wLength != 6) {
468 dev_err(priv_dev->dev, "Set SEL should be 6 bytes, got %d\n",
473 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 6, 1, 0);
478 * cdns3_req_ep0_set_isoch_delay -
479 * Handling of GET_ISOCH_DELAY standard USB request
480 * @priv_dev: extended gadget object
481 * @ctrl_req: pointer to received setup packet
483 * Returns 0 if success, error code on error
485 static int cdns3_req_ep0_set_isoch_delay(struct cdns3_device *priv_dev,
486 struct usb_ctrlrequest *ctrl_req)
488 if (ctrl_req->wIndex || ctrl_req->wLength)
491 priv_dev->isoch_delay = ctrl_req->wValue;
497 * cdns3_ep0_standard_request - Handling standard USB requests
498 * @priv_dev: extended gadget object
499 * @ctrl_req: pointer to received setup packet
501 * Returns 0 if success, error code on error
503 static int cdns3_ep0_standard_request(struct cdns3_device *priv_dev,
504 struct usb_ctrlrequest *ctrl_req)
508 switch (ctrl_req->bRequest) {
509 case USB_REQ_SET_ADDRESS:
510 ret = cdns3_req_ep0_set_address(priv_dev, ctrl_req);
512 case USB_REQ_SET_CONFIGURATION:
513 ret = cdns3_req_ep0_set_configuration(priv_dev, ctrl_req);
515 case USB_REQ_GET_STATUS:
516 ret = cdns3_req_ep0_get_status(priv_dev, ctrl_req);
518 case USB_REQ_CLEAR_FEATURE:
519 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 0);
521 case USB_REQ_SET_FEATURE:
522 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 1);
524 case USB_REQ_SET_SEL:
525 ret = cdns3_req_ep0_set_sel(priv_dev, ctrl_req);
527 case USB_REQ_SET_ISOCH_DELAY:
528 ret = cdns3_req_ep0_set_isoch_delay(priv_dev, ctrl_req);
531 ret = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
538 static void __pending_setup_status_handler(struct cdns3_device *priv_dev)
540 struct usb_request *request = priv_dev->pending_status_request;
542 if (priv_dev->status_completion_no_call && request &&
544 request->complete(&priv_dev->eps[0]->endpoint, request);
545 priv_dev->status_completion_no_call = 0;
549 void cdns3_pending_setup_status_handler(struct work_struct *work)
551 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
555 spin_lock_irqsave(&priv_dev->lock, flags);
556 __pending_setup_status_handler(priv_dev);
557 spin_unlock_irqrestore(&priv_dev->lock, flags);
561 * cdns3_ep0_setup_phase - Handling setup USB requests
562 * @priv_dev: extended gadget object
564 static void cdns3_ep0_setup_phase(struct cdns3_device *priv_dev)
566 struct usb_ctrlrequest *ctrl = priv_dev->setup_buf;
567 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
570 /* Invalidate Setup Packet received */
571 invalidate_dcache_range(priv_dev->setup_dma,
572 priv_dev->setup_dma + ARCH_DMA_MINALIGN);
574 priv_dev->ep0_data_dir = ctrl->bRequestType & USB_DIR_IN;
576 trace_cdns3_ctrl_req(ctrl);
578 if (!list_empty(&priv_ep->pending_req_list)) {
579 struct usb_request *request;
581 request = cdns3_next_request(&priv_ep->pending_req_list);
582 priv_ep->dir = priv_dev->ep0_data_dir;
583 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
587 if (le16_to_cpu(ctrl->wLength))
588 priv_dev->ep0_stage = CDNS3_DATA_STAGE;
590 priv_dev->ep0_stage = CDNS3_STATUS_STAGE;
592 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
593 result = cdns3_ep0_standard_request(priv_dev, ctrl);
595 result = cdns3_ep0_delegate_req(priv_dev, ctrl);
597 if (result == USB_GADGET_DELAYED_STATUS)
601 cdns3_ep0_complete_setup(priv_dev, 1, 1);
602 else if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE)
603 cdns3_ep0_complete_setup(priv_dev, 0, 1);
606 static void cdns3_transfer_completed(struct cdns3_device *priv_dev)
608 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
610 if (!list_empty(&priv_ep->pending_req_list)) {
611 struct usb_request *request;
613 trace_cdns3_complete_trb(priv_ep, priv_ep->trb_pool);
614 request = cdns3_next_request(&priv_ep->pending_req_list);
616 /* Invalidate TRB before accessing it */
617 invalidate_dcache_range((unsigned long)priv_ep->trb_pool,
618 (unsigned long)priv_ep->trb_pool +
619 ROUND(sizeof(struct cdns3_trb),
620 CONFIG_SYS_CACHELINE_SIZE));
623 TRB_LEN(le32_to_cpu(priv_ep->trb_pool->length));
625 priv_ep->dir = priv_dev->ep0_data_dir;
626 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 0);
629 cdns3_ep0_complete_setup(priv_dev, 0, 0);
633 * cdns3_check_new_setup - Check if controller receive new SETUP packet.
634 * @priv_dev: extended gadget object
636 * The SETUP packet can be kept in on-chip memory or in system memory.
638 static bool cdns3_check_new_setup(struct cdns3_device *priv_dev)
642 cdns3_select_ep(priv_dev, 0 | USB_DIR_OUT);
643 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
645 return !!(ep_sts_reg & (EP_STS_SETUP | EP_STS_STPWAIT));
649 * cdns3_check_ep0_interrupt_proceed - Processes interrupt related to endpoint 0
650 * @priv_dev: extended gadget object
651 * @dir: USB_DIR_IN for IN direction, USB_DIR_OUT for OUT direction
653 void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir)
657 cdns3_select_ep(priv_dev, dir);
659 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
660 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
662 trace_cdns3_ep0_irq(priv_dev, ep_sts_reg);
664 __pending_setup_status_handler(priv_dev);
666 if (ep_sts_reg & EP_STS_SETUP)
667 priv_dev->wait_for_setup = 1;
669 if (priv_dev->wait_for_setup && ep_sts_reg & EP_STS_IOC) {
670 priv_dev->wait_for_setup = 0;
671 cdns3_allow_enable_l1(priv_dev, 0);
672 cdns3_ep0_setup_phase(priv_dev);
673 } else if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP)) {
674 priv_dev->ep0_data_dir = dir;
675 cdns3_transfer_completed(priv_dev);
678 if (ep_sts_reg & EP_STS_DESCMIS) {
679 if (dir == 0 && !priv_dev->setup_pending)
680 cdns3_prepare_setup_packet(priv_dev);
685 * cdns3_gadget_ep0_enable
686 * Function shouldn't be called by gadget driver,
687 * endpoint 0 is allways active
689 static int cdns3_gadget_ep0_enable(struct usb_ep *ep,
690 const struct usb_endpoint_descriptor *desc)
696 * cdns3_gadget_ep0_disable
697 * Function shouldn't be called by gadget driver,
698 * endpoint 0 is allways active
700 static int cdns3_gadget_ep0_disable(struct usb_ep *ep)
706 * cdns3_gadget_ep0_set_halt
707 * @ep: pointer to endpoint zero object
708 * @value: 1 for set stall, 0 for clear stall
712 static int cdns3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
719 * cdns3_gadget_ep0_queue Transfer data on endpoint zero
720 * @ep: pointer to endpoint zero object
721 * @request: pointer to request object
722 * @gfp_flags: gfp flags
724 * Returns 0 on success, error code elsewhere
726 static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
727 struct usb_request *request,
730 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
731 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
737 trace_cdns3_ep0_queue(priv_dev, request);
739 /* cancel the request if controller receive new SETUP packet. */
740 if (cdns3_check_new_setup(priv_dev))
743 /* send STATUS stage. Should be called only for SET_CONFIGURATION */
744 if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) {
745 spin_lock_irqsave(&priv_dev->lock, flags);
746 cdns3_select_ep(priv_dev, 0x00);
748 erdy_sent = !priv_dev->hw_configured_flag;
749 cdns3_set_hw_configuration(priv_dev);
752 cdns3_ep0_complete_setup(priv_dev, 0, 1);
754 cdns3_allow_enable_l1(priv_dev, 1);
757 priv_dev->status_completion_no_call = true;
758 priv_dev->pending_status_request = request;
759 spin_unlock_irqrestore(&priv_dev->lock, flags);
762 * Since there is no completion interrupt for status stage,
763 * it needs to call ->completion in software after
767 queue_work(system_freezable_wq, &priv_dev->pending_status_wq);
769 __pending_setup_status_handler(priv_dev);
774 spin_lock_irqsave(&priv_dev->lock, flags);
775 if (!list_empty(&priv_ep->pending_req_list)) {
776 dev_err(priv_dev->dev,
777 "can't handle multiple requests for ep0\n");
778 spin_unlock_irqrestore(&priv_dev->lock, flags);
782 ret = usb_gadget_map_request(&priv_dev->gadget, request,
783 priv_dev->ep0_data_dir);
785 spin_unlock_irqrestore(&priv_dev->lock, flags);
786 dev_err(priv_dev->dev, "failed to map request\n");
790 request->status = -EINPROGRESS;
791 list_add_tail(&request->list, &priv_ep->pending_req_list);
793 if (request->zero && request->length &&
794 (request->length % ep->maxpacket == 0))
797 cdns3_ep0_run_transfer(priv_dev, request->dma, request->length, 1, zlp);
799 spin_unlock_irqrestore(&priv_dev->lock, flags);
805 * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint
806 * @ep: endpoint object
810 int cdns3_gadget_ep_set_wedge(struct usb_ep *ep)
812 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
814 dev_dbg(priv_ep->cdns3_dev->dev, "Wedge for %s\n", ep->name);
815 cdns3_gadget_ep_set_halt(ep, 1);
816 priv_ep->flags |= EP_WEDGE;
821 const struct usb_ep_ops cdns3_gadget_ep0_ops = {
822 .enable = cdns3_gadget_ep0_enable,
823 .disable = cdns3_gadget_ep0_disable,
824 .alloc_request = cdns3_gadget_ep_alloc_request,
825 .free_request = cdns3_gadget_ep_free_request,
826 .queue = cdns3_gadget_ep0_queue,
827 .dequeue = cdns3_gadget_ep_dequeue,
828 .set_halt = cdns3_gadget_ep0_set_halt,
829 .set_wedge = cdns3_gadget_ep_set_wedge,
833 * cdns3_ep0_config - Configures default endpoint
834 * @priv_dev: extended gadget object
836 * Functions sets parameters: maximal packet size and enables interrupts
838 void cdns3_ep0_config(struct cdns3_device *priv_dev)
840 struct cdns3_usb_regs __iomem *regs;
841 struct cdns3_endpoint *priv_ep;
842 u32 max_packet_size = 64;
844 regs = priv_dev->regs;
846 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
847 max_packet_size = 512;
849 priv_ep = priv_dev->eps[0];
851 if (!list_empty(&priv_ep->pending_req_list)) {
852 struct usb_request *request;
854 request = cdns3_next_request(&priv_ep->pending_req_list);
855 list_del_init(&request->list);
858 priv_dev->u1_allowed = 0;
859 priv_dev->u2_allowed = 0;
861 priv_dev->gadget.ep0->maxpacket = max_packet_size;
862 cdns3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(max_packet_size);
865 cdns3_select_ep(priv_dev, USB_DIR_OUT);
867 if (priv_dev->dev_ver >= DEV_VER_V3) {
868 cdns3_set_register_bit(&priv_dev->regs->dtrans,
870 cdns3_set_register_bit(&priv_dev->regs->tdl_from_trb,
874 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
877 writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN,
881 cdns3_select_ep(priv_dev, USB_DIR_IN);
883 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
886 writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, ®s->ep_sts_en);
888 cdns3_set_register_bit(®s->usb_conf, USB_CONF_U1DS | USB_CONF_U2DS);
892 * cdns3_init_ep0 Initializes software endpoint 0 of gadget
893 * @priv_dev: extended gadget object
894 * @ep_priv: extended endpoint object
896 * Returns 0 on success else error code.
898 int cdns3_init_ep0(struct cdns3_device *priv_dev,
899 struct cdns3_endpoint *priv_ep)
901 sprintf(priv_ep->name, "ep0");
903 /* fill linux fields */
904 priv_ep->endpoint.ops = &cdns3_gadget_ep0_ops;
905 priv_ep->endpoint.maxburst = 1;
906 usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
907 CDNS3_EP0_MAX_PACKET_LIMIT);
909 priv_ep->endpoint.address = 0;
911 priv_ep->endpoint.caps.type_control = 1;
912 priv_ep->endpoint.caps.dir_in = 1;
913 priv_ep->endpoint.caps.dir_out = 1;
914 priv_ep->endpoint.name = priv_ep->name;
915 priv_ep->endpoint.desc = &cdns3_gadget_ep0_desc;
916 priv_dev->gadget.ep0 = &priv_ep->endpoint;
917 priv_ep->type = USB_ENDPOINT_XFER_CONTROL;
919 return cdns3_allocate_trb_pool(priv_ep);