1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence USBHS-DEV driver.
5 * Copyright (C) 2023 Cadence Design Systems.
7 * Authors: Pawel Laszczak <pawell@cadence.com>
10 #include <linux/usb/composite.h>
11 #include <asm/unaligned.h>
13 #include "cdns2-gadget.h"
14 #include "cdns2-trace.h"
16 static struct usb_endpoint_descriptor cdns2_gadget_ep0_desc = {
17 .bLength = USB_DT_ENDPOINT_SIZE,
18 .bDescriptorType = USB_DT_ENDPOINT,
19 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
20 .wMaxPacketSize = cpu_to_le16(64)
23 static int cdns2_w_index_to_ep_index(u16 wIndex)
25 if (!(wIndex & USB_ENDPOINT_NUMBER_MASK))
28 return ((wIndex & USB_ENDPOINT_NUMBER_MASK) * 2) +
29 (wIndex & USB_ENDPOINT_DIR_MASK ? 1 : 0) - 1;
32 static bool cdns2_check_new_setup(struct cdns2_device *pdev)
36 reg = readb(&pdev->ep0_regs->cs);
38 return !!(reg & EP0CS_CHGSET);
41 static void cdns2_ep0_enqueue(struct cdns2_device *pdev, dma_addr_t dma_addr,
42 unsigned int length, int zlp)
44 struct cdns2_adma_regs __iomem *regs = pdev->adma_regs;
45 struct cdns2_endpoint *pep = &pdev->eps[0];
46 struct cdns2_ring *ring = &pep->ring;
48 ring->trbs[0].buffer = cpu_to_le32(TRB_BUFFER(dma_addr));
49 ring->trbs[0].length = cpu_to_le32(TRB_LEN(length));
52 ring->trbs[0].control = cpu_to_le32(TRB_CYCLE |
53 TRB_TYPE(TRB_NORMAL));
54 ring->trbs[1].buffer = cpu_to_le32(TRB_BUFFER(dma_addr));
55 ring->trbs[1].length = cpu_to_le32(TRB_LEN(0));
56 ring->trbs[1].control = cpu_to_le32(TRB_CYCLE | TRB_IOC |
57 TRB_TYPE(TRB_NORMAL));
59 ring->trbs[0].control = cpu_to_le32(TRB_CYCLE | TRB_IOC |
60 TRB_TYPE(TRB_NORMAL));
61 ring->trbs[1].control = 0;
64 trace_cdns2_queue_trb(pep, ring->trbs);
67 writel(0, &pdev->ep0_regs->rxbc);
69 cdns2_select_ep(pdev, pep->dir);
71 writel(DMA_EP_STS_TRBERR, ®s->ep_sts);
72 writel(pep->ring.dma, ®s->ep_traddr);
74 trace_cdns2_doorbell_ep0(pep, readl(®s->ep_traddr));
76 writel(DMA_EP_CMD_DRDY, ®s->ep_cmd);
79 static int cdns2_ep0_delegate_req(struct cdns2_device *pdev)
83 spin_unlock(&pdev->lock);
84 ret = pdev->gadget_driver->setup(&pdev->gadget, &pdev->setup);
85 spin_lock(&pdev->lock);
90 static void cdns2_ep0_stall(struct cdns2_device *pdev)
92 struct cdns2_endpoint *pep = &pdev->eps[0];
93 struct cdns2_request *preq;
95 preq = cdns2_next_preq(&pep->pending_list);
96 set_reg_bit_8(&pdev->ep0_regs->cs, EP0CS_DSTALL);
98 if (pdev->ep0_stage == CDNS2_DATA_STAGE && preq)
99 cdns2_gadget_giveback(pep, preq, -ECONNRESET);
101 list_del_init(&preq->list);
103 pdev->ep0_stage = CDNS2_SETUP_STAGE;
104 pep->ep_state |= EP_STALLED;
107 static void cdns2_status_stage(struct cdns2_device *pdev)
109 struct cdns2_endpoint *pep = &pdev->eps[0];
110 struct cdns2_request *preq;
112 preq = cdns2_next_preq(&pep->pending_list);
114 list_del_init(&preq->list);
116 pdev->ep0_stage = CDNS2_SETUP_STAGE;
117 writeb(EP0CS_HSNAK, &pdev->ep0_regs->cs);
120 static int cdns2_req_ep0_set_configuration(struct cdns2_device *pdev,
121 struct usb_ctrlrequest *ctrl_req)
123 enum usb_device_state state = pdev->gadget.state;
124 u32 config = le16_to_cpu(ctrl_req->wValue);
127 if (state < USB_STATE_ADDRESS) {
128 dev_err(pdev->dev, "Set Configuration - bad device state\n");
132 ret = cdns2_ep0_delegate_req(pdev);
136 trace_cdns2_device_state(config ? "configured" : "addressed");
139 usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
144 static int cdns2_req_ep0_set_address(struct cdns2_device *pdev, u32 addr)
146 enum usb_device_state device_state = pdev->gadget.state;
149 if (addr > USB_DEVICE_MAX_ADDRESS) {
151 "Device address (%d) cannot be greater than %d\n",
152 addr, USB_DEVICE_MAX_ADDRESS);
156 if (device_state == USB_STATE_CONFIGURED) {
158 "can't set_address from configured state\n");
162 reg = readb(&pdev->usb_regs->fnaddr);
163 pdev->dev_address = reg;
165 usb_gadget_set_state(&pdev->gadget,
166 (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT));
168 trace_cdns2_device_state(addr ? "addressed" : "default");
173 static int cdns2_req_ep0_handle_status(struct cdns2_device *pdev,
174 struct usb_ctrlrequest *ctrl)
176 struct cdns2_endpoint *pep;
177 __le16 *response_pkt;
182 recip = ctrl->bRequestType & USB_RECIP_MASK;
185 case USB_RECIP_DEVICE:
186 status = pdev->gadget.is_selfpowered;
187 status |= pdev->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
189 case USB_RECIP_INTERFACE:
190 return cdns2_ep0_delegate_req(pdev);
191 case USB_RECIP_ENDPOINT:
192 ep_sts = cdns2_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex));
193 pep = &pdev->eps[ep_sts];
195 if (pep->ep_state & EP_STALLED)
196 status = BIT(USB_ENDPOINT_HALT);
202 put_unaligned_le16(status, (__le16 *)pdev->ep0_preq.request.buf);
204 cdns2_ep0_enqueue(pdev, pdev->ep0_preq.request.dma,
205 sizeof(*response_pkt), 0);
210 static int cdns2_ep0_handle_feature_device(struct cdns2_device *pdev,
211 struct usb_ctrlrequest *ctrl,
214 enum usb_device_state state;
215 enum usb_device_speed speed;
220 wValue = le16_to_cpu(ctrl->wValue);
221 state = pdev->gadget.state;
222 speed = pdev->gadget.speed;
225 case USB_DEVICE_REMOTE_WAKEUP:
226 pdev->may_wakeup = !!set;
228 case USB_DEVICE_TEST_MODE:
229 if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
232 tmode = le16_to_cpu(ctrl->wIndex);
234 if (!set || (tmode & 0xff) != 0)
241 case USB_TEST_SE0_NAK:
242 case USB_TEST_PACKET:
244 * The USBHS controller automatically handles the
245 * Set_Feature(testmode) request. Standard test modes
246 * that use values of test mode selector from
247 * 01h to 04h (Test_J, Test_K, Test_SE0_NAK,
248 * Test_Packet) are supported by the
249 * controller(HS - ack, FS - stall).
263 static int cdns2_ep0_handle_feature_intf(struct cdns2_device *pdev,
264 struct usb_ctrlrequest *ctrl,
270 wValue = le16_to_cpu(ctrl->wValue);
273 case USB_INTRF_FUNC_SUSPEND:
282 static int cdns2_ep0_handle_feature_endpoint(struct cdns2_device *pdev,
283 struct usb_ctrlrequest *ctrl,
286 struct cdns2_endpoint *pep;
289 wValue = le16_to_cpu(ctrl->wValue);
290 pep = &pdev->eps[cdns2_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex))];
292 if (wValue != USB_ENDPOINT_HALT)
295 if (!(le16_to_cpu(ctrl->wIndex) & ~USB_DIR_IN))
299 case USB_ENDPOINT_HALT:
300 if (set || !(pep->ep_state & EP_WEDGE))
301 return cdns2_halt_endpoint(pdev, pep, set);
304 dev_warn(pdev->dev, "WARN Incorrect wValue %04x\n", wValue);
311 static int cdns2_req_ep0_handle_feature(struct cdns2_device *pdev,
312 struct usb_ctrlrequest *ctrl,
315 switch (ctrl->bRequestType & USB_RECIP_MASK) {
316 case USB_RECIP_DEVICE:
317 return cdns2_ep0_handle_feature_device(pdev, ctrl, set);
318 case USB_RECIP_INTERFACE:
319 return cdns2_ep0_handle_feature_intf(pdev, ctrl, set);
320 case USB_RECIP_ENDPOINT:
321 return cdns2_ep0_handle_feature_endpoint(pdev, ctrl, set);
327 static int cdns2_ep0_std_request(struct cdns2_device *pdev)
329 struct usb_ctrlrequest *ctrl = &pdev->setup;
332 switch (ctrl->bRequest) {
333 case USB_REQ_SET_ADDRESS:
334 ret = cdns2_req_ep0_set_address(pdev,
335 le16_to_cpu(ctrl->wValue));
337 case USB_REQ_SET_CONFIGURATION:
338 ret = cdns2_req_ep0_set_configuration(pdev, ctrl);
340 case USB_REQ_GET_STATUS:
341 ret = cdns2_req_ep0_handle_status(pdev, ctrl);
343 case USB_REQ_CLEAR_FEATURE:
344 ret = cdns2_req_ep0_handle_feature(pdev, ctrl, 0);
346 case USB_REQ_SET_FEATURE:
347 ret = cdns2_req_ep0_handle_feature(pdev, ctrl, 1);
350 ret = cdns2_ep0_delegate_req(pdev);
357 static void __pending_setup_status_handler(struct cdns2_device *pdev)
359 struct usb_request *request = pdev->pending_status_request;
361 if (pdev->status_completion_no_call && request && request->complete) {
362 request->complete(&pdev->eps[0].endpoint, request);
363 pdev->status_completion_no_call = 0;
367 void cdns2_pending_setup_status_handler(struct work_struct *work)
369 struct cdns2_device *pdev = container_of(work, struct cdns2_device,
373 spin_lock_irqsave(&pdev->lock, flags);
374 __pending_setup_status_handler(pdev);
375 spin_unlock_irqrestore(&pdev->lock, flags);
378 void cdns2_handle_setup_packet(struct cdns2_device *pdev)
380 struct usb_ctrlrequest *ctrl = &pdev->setup;
381 struct cdns2_endpoint *pep = &pdev->eps[0];
382 struct cdns2_request *preq;
388 writeb(EP0CS_CHGSET, &pdev->ep0_regs->cs);
390 for (i = 0; i < 8; i++)
391 ((u8 *)&pdev->setup)[i] = readb(&pdev->ep0_regs->setupdat[i]);
394 * If SETUP packet was modified while reading just simple ignore it.
395 * The new one will be handled latter.
397 if (cdns2_check_new_setup(pdev)) {
398 trace_cdns2_ep0_setup("overridden");
402 trace_cdns2_ctrl_req(ctrl);
404 if (!pdev->gadget_driver)
407 if (pdev->gadget.state == USB_STATE_NOTATTACHED) {
408 dev_err(pdev->dev, "ERR: Setup detected in unattached state\n");
415 /* Halt for Ep0 is cleared automatically when SETUP packet arrives. */
416 pep->ep_state &= ~EP_STALLED;
418 if (!list_empty(&pep->pending_list)) {
419 preq = cdns2_next_preq(&pep->pending_list);
420 cdns2_gadget_giveback(pep, preq, -ECONNRESET);
423 len = le16_to_cpu(ctrl->wLength);
425 pdev->ep0_stage = CDNS2_DATA_STAGE;
427 pdev->ep0_stage = CDNS2_STATUS_STAGE;
429 pep->dir = ctrl->bRequestType & USB_DIR_IN;
432 * SET_ADDRESS request is acknowledged automatically by controller and
433 * in the worse case driver may not notice this request. To check
434 * whether this request has been processed driver can use
437 reg = readb(&pdev->usb_regs->fnaddr);
438 if (pdev->setup.bRequest != USB_REQ_SET_ADDRESS &&
439 pdev->dev_address != reg)
440 cdns2_req_ep0_set_address(pdev, reg);
442 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
443 ret = cdns2_ep0_std_request(pdev);
445 ret = cdns2_ep0_delegate_req(pdev);
447 if (ret == USB_GADGET_DELAYED_STATUS) {
448 trace_cdns2_ep0_status_stage("delayed");
454 cdns2_ep0_stall(pdev);
455 else if (pdev->ep0_stage == CDNS2_STATUS_STAGE)
456 cdns2_status_stage(pdev);
459 static void cdns2_transfer_completed(struct cdns2_device *pdev)
461 struct cdns2_endpoint *pep = &pdev->eps[0];
463 if (!list_empty(&pep->pending_list)) {
464 struct cdns2_request *preq;
466 trace_cdns2_complete_trb(pep, pep->ring.trbs);
467 preq = cdns2_next_preq(&pep->pending_list);
469 preq->request.actual =
470 TRB_LEN(le32_to_cpu(pep->ring.trbs->length));
471 cdns2_gadget_giveback(pep, preq, 0);
474 cdns2_status_stage(pdev);
477 void cdns2_handle_ep0_interrupt(struct cdns2_device *pdev, int dir)
481 cdns2_select_ep(pdev, dir);
483 trace_cdns2_ep0_irq(pdev);
485 ep_sts_reg = readl(&pdev->adma_regs->ep_sts);
486 writel(ep_sts_reg, &pdev->adma_regs->ep_sts);
488 __pending_setup_status_handler(pdev);
490 if ((ep_sts_reg & DMA_EP_STS_IOC) || (ep_sts_reg & DMA_EP_STS_ISP)) {
491 pdev->eps[0].dir = dir;
492 cdns2_transfer_completed(pdev);
497 * Function shouldn't be called by gadget driver,
498 * endpoint 0 is allways active.
500 static int cdns2_gadget_ep0_enable(struct usb_ep *ep,
501 const struct usb_endpoint_descriptor *desc)
507 * Function shouldn't be called by gadget driver,
508 * endpoint 0 is allways active.
510 static int cdns2_gadget_ep0_disable(struct usb_ep *ep)
515 static int cdns2_gadget_ep0_set_halt(struct usb_ep *ep, int value)
517 struct cdns2_endpoint *pep = ep_to_cdns2_ep(ep);
518 struct cdns2_device *pdev = pep->pdev;
524 spin_lock_irqsave(&pdev->lock, flags);
525 cdns2_ep0_stall(pdev);
526 spin_unlock_irqrestore(&pdev->lock, flags);
531 static int cdns2_gadget_ep0_set_wedge(struct usb_ep *ep)
533 return cdns2_gadget_ep0_set_halt(ep, 1);
536 static int cdns2_gadget_ep0_queue(struct usb_ep *ep,
537 struct usb_request *request,
540 struct cdns2_endpoint *pep = ep_to_cdns2_ep(ep);
541 struct cdns2_device *pdev = pep->pdev;
542 struct cdns2_request *preq;
547 spin_lock_irqsave(&pdev->lock, flags);
549 preq = to_cdns2_request(request);
551 trace_cdns2_request_enqueue(preq);
553 /* Cancel the request if controller receive new SETUP packet. */
554 if (cdns2_check_new_setup(pdev)) {
555 trace_cdns2_ep0_setup("overridden");
556 spin_unlock_irqrestore(&pdev->lock, flags);
560 /* Send STATUS stage. Should be called only for SET_CONFIGURATION. */
561 if (pdev->ep0_stage == CDNS2_STATUS_STAGE) {
562 cdns2_status_stage(pdev);
565 pdev->status_completion_no_call = true;
566 pdev->pending_status_request = request;
567 usb_gadget_set_state(&pdev->gadget, USB_STATE_CONFIGURED);
568 spin_unlock_irqrestore(&pdev->lock, flags);
571 * Since there is no completion interrupt for status stage,
572 * it needs to call ->completion in software after
573 * cdns2_gadget_ep0_queue is back.
575 queue_work(system_freezable_wq, &pdev->pending_status_wq);
579 if (!list_empty(&pep->pending_list)) {
580 trace_cdns2_ep0_setup("pending");
582 "can't handle multiple requests for ep0\n");
583 spin_unlock_irqrestore(&pdev->lock, flags);
587 ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->dir);
589 spin_unlock_irqrestore(&pdev->lock, flags);
590 dev_err(pdev->dev, "failed to map request\n");
594 request->status = -EINPROGRESS;
595 list_add_tail(&preq->list, &pep->pending_list);
597 if (request->zero && request->length &&
598 (request->length % ep->maxpacket == 0))
601 cdns2_ep0_enqueue(pdev, request->dma, request->length, zlp);
603 spin_unlock_irqrestore(&pdev->lock, flags);
608 static const struct usb_ep_ops cdns2_gadget_ep0_ops = {
609 .enable = cdns2_gadget_ep0_enable,
610 .disable = cdns2_gadget_ep0_disable,
611 .alloc_request = cdns2_gadget_ep_alloc_request,
612 .free_request = cdns2_gadget_ep_free_request,
613 .queue = cdns2_gadget_ep0_queue,
614 .dequeue = cdns2_gadget_ep_dequeue,
615 .set_halt = cdns2_gadget_ep0_set_halt,
616 .set_wedge = cdns2_gadget_ep0_set_wedge,
619 void cdns2_ep0_config(struct cdns2_device *pdev)
621 struct cdns2_endpoint *pep;
625 if (!list_empty(&pep->pending_list)) {
626 struct cdns2_request *preq;
628 preq = cdns2_next_preq(&pep->pending_list);
629 list_del_init(&preq->list);
632 writeb(EP0_FIFO_AUTO, &pdev->ep0_regs->fifo);
633 cdns2_select_ep(pdev, USB_DIR_OUT);
634 writel(DMA_EP_CFG_ENABLE, &pdev->adma_regs->ep_cfg);
636 writeb(EP0_FIFO_IO_TX | EP0_FIFO_AUTO, &pdev->ep0_regs->fifo);
637 cdns2_select_ep(pdev, USB_DIR_IN);
638 writel(DMA_EP_CFG_ENABLE, &pdev->adma_regs->ep_cfg);
640 writeb(pdev->gadget.ep0->maxpacket, &pdev->ep0_regs->maxpack);
641 writel(DMA_EP_IEN_EP_OUT0 | DMA_EP_IEN_EP_IN0,
642 &pdev->adma_regs->ep_ien);
645 void cdns2_init_ep0(struct cdns2_device *pdev,
646 struct cdns2_endpoint *pep)
648 u16 maxpacket = le16_to_cpu(cdns2_gadget_ep0_desc.wMaxPacketSize);
650 usb_ep_set_maxpacket_limit(&pep->endpoint, maxpacket);
652 pep->endpoint.ops = &cdns2_gadget_ep0_ops;
653 pep->endpoint.desc = &cdns2_gadget_ep0_desc;
654 pep->endpoint.caps.type_control = true;
655 pep->endpoint.caps.dir_in = true;
656 pep->endpoint.caps.dir_out = true;
658 pdev->gadget.ep0 = &pep->endpoint;