1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Cadence USBHS-DEV driver.
4 *
5 * Copyright (C) 2023 Cadence Design Systems.
6 *
7 * Authors: Pawel Laszczak <pawell@cadence.com>
8 */
9
10#include <linux/usb/composite.h>
11#include <asm/unaligned.h>
12
13#include "cdns2-gadget.h"
14#include "cdns2-trace.h"
15
16static 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)
21};
22
23static int cdns2_w_index_to_ep_index(u16 wIndex)
24{
25	if (!(wIndex & USB_ENDPOINT_NUMBER_MASK))
26		return 0;
27
28	return ((wIndex & USB_ENDPOINT_NUMBER_MASK) * 2) +
29		(wIndex & USB_ENDPOINT_DIR_MASK ? 1 : 0) - 1;
30}
31
32static bool cdns2_check_new_setup(struct cdns2_device *pdev)
33{
34	u8 reg;
35
36	reg = readb(&pdev->ep0_regs->cs);
37
38	return !!(reg & EP0CS_CHGSET);
39}
40
41static void cdns2_ep0_enqueue(struct cdns2_device *pdev, dma_addr_t dma_addr,
42			      unsigned int length, int zlp)
43{
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;
47
48	ring->trbs[0].buffer = cpu_to_le32(TRB_BUFFER(dma_addr));
49	ring->trbs[0].length = cpu_to_le32(TRB_LEN(length));
50
51	if (zlp) {
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));
58	} else {
59		ring->trbs[0].control = cpu_to_le32(TRB_CYCLE | TRB_IOC |
60					TRB_TYPE(TRB_NORMAL));
61		ring->trbs[1].control = 0;
62	}
63
64	trace_cdns2_queue_trb(pep, ring->trbs);
65
66	if (!pep->dir)
67		writel(0, &pdev->ep0_regs->rxbc);
68
69	cdns2_select_ep(pdev, pep->dir);
70
71	writel(DMA_EP_STS_TRBERR, &regs->ep_sts);
72	writel(pep->ring.dma, &regs->ep_traddr);
73
74	trace_cdns2_doorbell_ep0(pep, readl(&regs->ep_traddr));
75
76	writel(DMA_EP_CMD_DRDY, &regs->ep_cmd);
77}
78
79static int cdns2_ep0_delegate_req(struct cdns2_device *pdev)
80{
81	int ret;
82
83	spin_unlock(&pdev->lock);
84	ret = pdev->gadget_driver->setup(&pdev->gadget, &pdev->setup);
85	spin_lock(&pdev->lock);
86
87	return ret;
88}
89
90static void cdns2_ep0_stall(struct cdns2_device *pdev)
91{
92	struct cdns2_endpoint *pep = &pdev->eps[0];
93	struct cdns2_request *preq;
94
95	preq = cdns2_next_preq(&pep->pending_list);
96	set_reg_bit_8(&pdev->ep0_regs->cs, EP0CS_DSTALL);
97
98	if (pdev->ep0_stage == CDNS2_DATA_STAGE && preq)
99		cdns2_gadget_giveback(pep, preq, -ECONNRESET);
100	else if (preq)
101		list_del_init(&preq->list);
102
103	pdev->ep0_stage = CDNS2_SETUP_STAGE;
104	pep->ep_state |= EP_STALLED;
105}
106
107static void cdns2_status_stage(struct cdns2_device *pdev)
108{
109	struct cdns2_endpoint *pep = &pdev->eps[0];
110	struct cdns2_request *preq;
111
112	preq = cdns2_next_preq(&pep->pending_list);
113	if (preq)
114		list_del_init(&preq->list);
115
116	pdev->ep0_stage = CDNS2_SETUP_STAGE;
117	writeb(EP0CS_HSNAK, &pdev->ep0_regs->cs);
118}
119
120static int cdns2_req_ep0_set_configuration(struct cdns2_device *pdev,
121					   struct usb_ctrlrequest *ctrl_req)
122{
123	enum usb_device_state state = pdev->gadget.state;
124	u32 config = le16_to_cpu(ctrl_req->wValue);
125	int ret;
126
127	if (state < USB_STATE_ADDRESS) {
128		dev_err(pdev->dev, "Set Configuration - bad device state\n");
129		return -EINVAL;
130	}
131
132	ret = cdns2_ep0_delegate_req(pdev);
133	if (ret)
134		return ret;
135
136	trace_cdns2_device_state(config ? "configured" : "addressed");
137
138	if (!config)
139		usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
140
141	return 0;
142}
143
144static int cdns2_req_ep0_set_address(struct cdns2_device *pdev, u32 addr)
145{
146	enum usb_device_state device_state = pdev->gadget.state;
147	u8 reg;
148
149	if (addr > USB_DEVICE_MAX_ADDRESS) {
150		dev_err(pdev->dev,
151			"Device address (%d) cannot be greater than %d\n",
152			addr, USB_DEVICE_MAX_ADDRESS);
153		return -EINVAL;
154	}
155
156	if (device_state == USB_STATE_CONFIGURED) {
157		dev_err(pdev->dev,
158			"can't set_address from configured state\n");
159		return -EINVAL;
160	}
161
162	reg = readb(&pdev->usb_regs->fnaddr);
163	pdev->dev_address = reg;
164
165	usb_gadget_set_state(&pdev->gadget,
166			     (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT));
167
168	trace_cdns2_device_state(addr ? "addressed" : "default");
169
170	return 0;
171}
172
173static int cdns2_req_ep0_handle_status(struct cdns2_device *pdev,
174				       struct usb_ctrlrequest *ctrl)
175{
176	struct cdns2_endpoint *pep;
177	__le16 *response_pkt;
178	u16 status = 0;
179	int ep_sts;
180	u32 recip;
181
182	recip = ctrl->bRequestType & USB_RECIP_MASK;
183
184	switch (recip) {
185	case USB_RECIP_DEVICE:
186		status = pdev->gadget.is_selfpowered;
187		status |= pdev->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
188		break;
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];
194
195		if (pep->ep_state & EP_STALLED)
196			status =  BIT(USB_ENDPOINT_HALT);
197		break;
198	default:
199		return -EINVAL;
200	}
201
202	put_unaligned_le16(status, (__le16 *)pdev->ep0_preq.request.buf);
203
204	cdns2_ep0_enqueue(pdev, pdev->ep0_preq.request.dma,
205			  sizeof(*response_pkt), 0);
206
207	return 0;
208}
209
210static int cdns2_ep0_handle_feature_device(struct cdns2_device *pdev,
211					   struct usb_ctrlrequest *ctrl,
212					   int set)
213{
214	enum usb_device_state state;
215	enum usb_device_speed speed;
216	int ret = 0;
217	u32 wValue;
218	u16 tmode;
219
220	wValue = le16_to_cpu(ctrl->wValue);
221	state = pdev->gadget.state;
222	speed = pdev->gadget.speed;
223
224	switch (wValue) {
225	case USB_DEVICE_REMOTE_WAKEUP:
226		pdev->may_wakeup = !!set;
227		break;
228	case USB_DEVICE_TEST_MODE:
229		if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
230			return -EINVAL;
231
232		tmode = le16_to_cpu(ctrl->wIndex);
233
234		if (!set || (tmode & 0xff) != 0)
235			return -EINVAL;
236
237		tmode >>= 8;
238		switch (tmode) {
239		case USB_TEST_J:
240		case USB_TEST_K:
241		case USB_TEST_SE0_NAK:
242		case USB_TEST_PACKET:
243			/*
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).
250			 */
251			break;
252		default:
253			ret = -EINVAL;
254		}
255		break;
256	default:
257		ret = -EINVAL;
258	}
259
260	return ret;
261}
262
263static int cdns2_ep0_handle_feature_intf(struct cdns2_device *pdev,
264					 struct usb_ctrlrequest *ctrl,
265					 int set)
266{
267	int ret = 0;
268	u32 wValue;
269
270	wValue = le16_to_cpu(ctrl->wValue);
271
272	switch (wValue) {
273	case USB_INTRF_FUNC_SUSPEND:
274		break;
275	default:
276		ret = -EINVAL;
277	}
278
279	return ret;
280}
281
282static int cdns2_ep0_handle_feature_endpoint(struct cdns2_device *pdev,
283					     struct usb_ctrlrequest *ctrl,
284					     int set)
285{
286	struct cdns2_endpoint *pep;
287	u8 wValue;
288
289	wValue = le16_to_cpu(ctrl->wValue);
290	pep = &pdev->eps[cdns2_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex))];
291
292	if (wValue != USB_ENDPOINT_HALT)
293		return -EINVAL;
294
295	if (!(le16_to_cpu(ctrl->wIndex) & ~USB_DIR_IN))
296		return 0;
297
298	switch (wValue) {
299	case USB_ENDPOINT_HALT:
300		if (set || !(pep->ep_state & EP_WEDGE))
301			return cdns2_halt_endpoint(pdev, pep, set);
302		break;
303	default:
304		dev_warn(pdev->dev, "WARN Incorrect wValue %04x\n", wValue);
305		return -EINVAL;
306	}
307
308	return 0;
309}
310
311static int cdns2_req_ep0_handle_feature(struct cdns2_device *pdev,
312					struct usb_ctrlrequest *ctrl,
313					int set)
314{
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);
322	default:
323		return -EINVAL;
324	}
325}
326
327static int cdns2_ep0_std_request(struct cdns2_device *pdev)
328{
329	struct usb_ctrlrequest *ctrl = &pdev->setup;
330	int ret;
331
332	switch (ctrl->bRequest) {
333	case USB_REQ_SET_ADDRESS:
334		ret = cdns2_req_ep0_set_address(pdev,
335						le16_to_cpu(ctrl->wValue));
336		break;
337	case USB_REQ_SET_CONFIGURATION:
338		ret = cdns2_req_ep0_set_configuration(pdev, ctrl);
339		break;
340	case USB_REQ_GET_STATUS:
341		ret = cdns2_req_ep0_handle_status(pdev, ctrl);
342		break;
343	case USB_REQ_CLEAR_FEATURE:
344		ret = cdns2_req_ep0_handle_feature(pdev, ctrl, 0);
345		break;
346	case USB_REQ_SET_FEATURE:
347		ret = cdns2_req_ep0_handle_feature(pdev, ctrl, 1);
348		break;
349	default:
350		ret = cdns2_ep0_delegate_req(pdev);
351		break;
352	}
353
354	return ret;
355}
356
357static void __pending_setup_status_handler(struct cdns2_device *pdev)
358{
359	struct usb_request *request = pdev->pending_status_request;
360
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;
364	}
365}
366
367void cdns2_pending_setup_status_handler(struct work_struct *work)
368{
369	struct cdns2_device *pdev = container_of(work, struct cdns2_device,
370						 pending_status_wq);
371	unsigned long flags;
372
373	spin_lock_irqsave(&pdev->lock, flags);
374	__pending_setup_status_handler(pdev);
375	spin_unlock_irqrestore(&pdev->lock, flags);
376}
377
378void cdns2_handle_setup_packet(struct cdns2_device *pdev)
379{
380	struct usb_ctrlrequest *ctrl = &pdev->setup;
381	struct cdns2_endpoint *pep = &pdev->eps[0];
382	struct cdns2_request *preq;
383	int ret = 0;
384	u16 len;
385	u8 reg;
386	int i;
387
388	writeb(EP0CS_CHGSET, &pdev->ep0_regs->cs);
389
390	for (i = 0; i < 8; i++)
391		((u8 *)&pdev->setup)[i] = readb(&pdev->ep0_regs->setupdat[i]);
392
393	/*
394	 * If SETUP packet was modified while reading just simple ignore it.
395	 * The new one will be handled latter.
396	 */
397	if (cdns2_check_new_setup(pdev)) {
398		trace_cdns2_ep0_setup("overridden");
399		return;
400	}
401
402	trace_cdns2_ctrl_req(ctrl);
403
404	if (!pdev->gadget_driver)
405		goto out;
406
407	if (pdev->gadget.state == USB_STATE_NOTATTACHED) {
408		dev_err(pdev->dev, "ERR: Setup detected in unattached state\n");
409		ret = -EINVAL;
410		goto out;
411	}
412
413	pep = &pdev->eps[0];
414
415	/* Halt for Ep0 is cleared automatically when SETUP packet arrives. */
416	pep->ep_state &= ~EP_STALLED;
417
418	if (!list_empty(&pep->pending_list)) {
419		preq = cdns2_next_preq(&pep->pending_list);
420		cdns2_gadget_giveback(pep, preq, -ECONNRESET);
421	}
422
423	len = le16_to_cpu(ctrl->wLength);
424	if (len)
425		pdev->ep0_stage = CDNS2_DATA_STAGE;
426	else
427		pdev->ep0_stage = CDNS2_STATUS_STAGE;
428
429	pep->dir = ctrl->bRequestType & USB_DIR_IN;
430
431	/*
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
435	 * fnaddr register.
436	 */
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);
441
442	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
443		ret = cdns2_ep0_std_request(pdev);
444	else
445		ret = cdns2_ep0_delegate_req(pdev);
446
447	if (ret == USB_GADGET_DELAYED_STATUS) {
448		trace_cdns2_ep0_status_stage("delayed");
449		return;
450	}
451
452out:
453	if (ret < 0)
454		cdns2_ep0_stall(pdev);
455	else if (pdev->ep0_stage == CDNS2_STATUS_STAGE)
456		cdns2_status_stage(pdev);
457}
458
459static void cdns2_transfer_completed(struct cdns2_device *pdev)
460{
461	struct cdns2_endpoint *pep = &pdev->eps[0];
462
463	if (!list_empty(&pep->pending_list)) {
464		struct cdns2_request *preq;
465
466		trace_cdns2_complete_trb(pep, pep->ring.trbs);
467		preq = cdns2_next_preq(&pep->pending_list);
468
469		preq->request.actual =
470			TRB_LEN(le32_to_cpu(pep->ring.trbs->length));
471		cdns2_gadget_giveback(pep, preq, 0);
472	}
473
474	cdns2_status_stage(pdev);
475}
476
477void cdns2_handle_ep0_interrupt(struct cdns2_device *pdev, int dir)
478{
479	u32 ep_sts_reg;
480
481	cdns2_select_ep(pdev, dir);
482
483	trace_cdns2_ep0_irq(pdev);
484
485	ep_sts_reg = readl(&pdev->adma_regs->ep_sts);
486	writel(ep_sts_reg, &pdev->adma_regs->ep_sts);
487
488	__pending_setup_status_handler(pdev);
489
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);
493	}
494}
495
496/*
497 * Function shouldn't be called by gadget driver,
498 * endpoint 0 is allways active.
499 */
500static int cdns2_gadget_ep0_enable(struct usb_ep *ep,
501				   const struct usb_endpoint_descriptor *desc)
502{
503	return -EINVAL;
504}
505
506/*
507 * Function shouldn't be called by gadget driver,
508 * endpoint 0 is allways active.
509 */
510static int cdns2_gadget_ep0_disable(struct usb_ep *ep)
511{
512	return -EINVAL;
513}
514
515static int cdns2_gadget_ep0_set_halt(struct usb_ep *ep, int value)
516{
517	struct cdns2_endpoint *pep = ep_to_cdns2_ep(ep);
518	struct cdns2_device *pdev = pep->pdev;
519	unsigned long flags;
520
521	if (!value)
522		return 0;
523
524	spin_lock_irqsave(&pdev->lock, flags);
525	cdns2_ep0_stall(pdev);
526	spin_unlock_irqrestore(&pdev->lock, flags);
527
528	return 0;
529}
530
531static int cdns2_gadget_ep0_set_wedge(struct usb_ep *ep)
532{
533	return cdns2_gadget_ep0_set_halt(ep, 1);
534}
535
536static int cdns2_gadget_ep0_queue(struct usb_ep *ep,
537				  struct usb_request *request,
538				  gfp_t gfp_flags)
539{
540	struct cdns2_endpoint *pep = ep_to_cdns2_ep(ep);
541	struct cdns2_device *pdev = pep->pdev;
542	struct cdns2_request *preq;
543	unsigned long flags;
544	u8 zlp = 0;
545	int ret;
546
547	spin_lock_irqsave(&pdev->lock, flags);
548
549	preq = to_cdns2_request(request);
550
551	trace_cdns2_request_enqueue(preq);
552
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);
557		return -ECONNRESET;
558	}
559
560	/* Send STATUS stage. Should be called only for SET_CONFIGURATION. */
561	if (pdev->ep0_stage == CDNS2_STATUS_STAGE) {
562		cdns2_status_stage(pdev);
563
564		request->actual = 0;
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);
569
570		/*
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.
574		 */
575		queue_work(system_freezable_wq, &pdev->pending_status_wq);
576		return 0;
577	}
578
579	if (!list_empty(&pep->pending_list)) {
580		trace_cdns2_ep0_setup("pending");
581		dev_err(pdev->dev,
582			"can't handle multiple requests for ep0\n");
583		spin_unlock_irqrestore(&pdev->lock, flags);
584		return -EBUSY;
585	}
586
587	ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->dir);
588	if (ret) {
589		spin_unlock_irqrestore(&pdev->lock, flags);
590		dev_err(pdev->dev, "failed to map request\n");
591		return -EINVAL;
592	}
593
594	request->status = -EINPROGRESS;
595	list_add_tail(&preq->list, &pep->pending_list);
596
597	if (request->zero && request->length &&
598	    (request->length % ep->maxpacket == 0))
599		zlp = 1;
600
601	cdns2_ep0_enqueue(pdev, request->dma, request->length, zlp);
602
603	spin_unlock_irqrestore(&pdev->lock, flags);
604
605	return 0;
606}
607
608static 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,
617};
618
619void cdns2_ep0_config(struct cdns2_device *pdev)
620{
621	struct cdns2_endpoint *pep;
622
623	pep = &pdev->eps[0];
624
625	if (!list_empty(&pep->pending_list)) {
626		struct cdns2_request *preq;
627
628		preq = cdns2_next_preq(&pep->pending_list);
629		list_del_init(&preq->list);
630	}
631
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);
635
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);
639
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);
643}
644
645void cdns2_init_ep0(struct cdns2_device *pdev,
646		    struct cdns2_endpoint *pep)
647{
648	u16 maxpacket = le16_to_cpu(cdns2_gadget_ep0_desc.wMaxPacketSize);
649
650	usb_ep_set_maxpacket_limit(&pep->endpoint, maxpacket);
651
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;
657
658	pdev->gadget.ep0 = &pep->endpoint;
659}
660