1
2
3
4
5
6
7
8
9
10
11
12
13#include <cpu_func.h>
14#include <dm.h>
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>
21
22#include "gadget.h"
23#include "trace.h"
24
25#define readl_poll_timeout_atomic readl_poll_timeout
26#define usleep_range(a, b) udelay((b))
27
28static 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,
32};
33
34
35
36
37
38
39
40
41
42static void cdns3_ep0_run_transfer(struct cdns3_device *priv_dev,
43 dma_addr_t dma_addr,
44 unsigned int length, int erdy, int zlp)
45{
46 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
47 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
48
49 priv_ep->trb_pool[0].buffer = TRB_BUFFER(dma_addr);
50 priv_ep->trb_pool[0].length = TRB_LEN(length);
51
52 if (zlp) {
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 |
57 TRB_TYPE(TRB_NORMAL);
58 } else {
59 priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_IOC |
60 TRB_TYPE(TRB_NORMAL);
61 priv_ep->trb_pool[1].control = 0;
62 }
63
64
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));
69
70 trace_cdns3_prepare_trb(priv_ep, priv_ep->trb_pool);
71
72 cdns3_select_ep(priv_dev, priv_dev->ep0_data_dir);
73
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));
78
79
80 writel(EP_CMD_DRDY, ®s->ep_cmd);
81
82
83 __cdns3_gadget_wakeup(priv_dev);
84
85 if (erdy)
86 writel(EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
87}
88
89
90
91
92
93
94
95
96
97static int cdns3_ep0_delegate_req(struct cdns3_device *priv_dev,
98 struct usb_ctrlrequest *ctrl_req)
99{
100 int ret;
101
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);
107 return ret;
108}
109
110static void cdns3_prepare_setup_packet(struct cdns3_device *priv_dev)
111{
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);
116}
117
118static void cdns3_ep0_complete_setup(struct cdns3_device *priv_dev,
119 u8 send_stall, u8 send_erdy)
120{
121 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
122 struct usb_request *request;
123
124 request = cdns3_next_request(&priv_ep->pending_req_list);
125 if (request)
126 list_del_init(&request->list);
127
128 if (send_stall) {
129 trace_cdns3_halt(priv_ep, send_stall, 0);
130
131 cdns3_select_ep(priv_dev, 0x00);
132 writel(EP_CMD_SSTALL, &priv_dev->regs->ep_cmd);
133 } else {
134 cdns3_prepare_setup_packet(priv_dev);
135 }
136
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);
140
141 cdns3_allow_enable_l1(priv_dev, 1);
142}
143
144
145
146
147
148
149
150
151
152static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev,
153 struct usb_ctrlrequest *ctrl_req)
154{
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);
158 int result = 0;
159 int i;
160
161 switch (device_state) {
162 case USB_STATE_ADDRESS:
163
164 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
165 priv_ep = priv_dev->eps[i];
166 if (!priv_ep)
167 continue;
168
169 if (priv_ep->flags & EP_CLAIMED)
170 cdns3_ep_config(priv_ep);
171 }
172
173 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
174
175 if (result)
176 return result;
177
178 if (config) {
179 cdns3_set_hw_configuration(priv_dev);
180 } else {
181 cdns3_hw_reset_eps_config(priv_dev);
182 usb_gadget_set_state(&priv_dev->gadget,
183 USB_STATE_ADDRESS);
184 }
185 break;
186 case USB_STATE_CONFIGURED:
187 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
188
189 if (!config && !result) {
190 cdns3_hw_reset_eps_config(priv_dev);
191 usb_gadget_set_state(&priv_dev->gadget,
192 USB_STATE_ADDRESS);
193 }
194 break;
195 default:
196 result = -EINVAL;
197 }
198
199 return result;
200}
201
202
203
204
205
206
207
208
209static int cdns3_req_ep0_set_address(struct cdns3_device *priv_dev,
210 struct usb_ctrlrequest *ctrl_req)
211{
212 enum usb_device_state device_state = priv_dev->gadget.state;
213 u32 reg;
214 u32 addr;
215
216 addr = le16_to_cpu(ctrl_req->wValue);
217
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);
222 return -EINVAL;
223 }
224
225 if (device_state == USB_STATE_CONFIGURED) {
226 dev_err(priv_dev->dev,
227 "can't set_address from configured state\n");
228 return -EINVAL;
229 }
230
231 reg = readl(&priv_dev->regs->usb_cmd);
232
233 writel(reg | USB_CMD_FADDR(addr) | USB_CMD_SET_ADDR,
234 &priv_dev->regs->usb_cmd);
235
236 usb_gadget_set_state(&priv_dev->gadget,
237 (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT));
238
239 return 0;
240}
241
242
243
244
245
246
247
248
249static int cdns3_req_ep0_get_status(struct cdns3_device *priv_dev,
250 struct usb_ctrlrequest *ctrl)
251{
252 __le16 *response_pkt;
253 u16 usb_status = 0;
254 u32 recip;
255
256 recip = ctrl->bRequestType & USB_RECIP_MASK;
257
258 switch (recip) {
259 case USB_RECIP_DEVICE:
260
261 if (priv_dev->is_selfpowered)
262 usb_status = BIT(USB_DEVICE_SELF_POWERED);
263
264 if (priv_dev->wake_up_flag)
265 usb_status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
266
267 if (priv_dev->gadget.speed != USB_SPEED_SUPER)
268 break;
269
270 if (priv_dev->u1_allowed)
271 usb_status |= BIT(USB_DEV_STAT_U1_ENABLED);
272
273 if (priv_dev->u2_allowed)
274 usb_status |= BIT(USB_DEV_STAT_U2_ENABLED);
275
276 break;
277 case USB_RECIP_INTERFACE:
278 return cdns3_ep0_delegate_req(priv_dev, ctrl);
279 case USB_RECIP_ENDPOINT:
280
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);
284 break;
285 default:
286 return -EINVAL;
287 }
288
289 response_pkt = (__le16 *)priv_dev->setup_buf;
290 *response_pkt = cpu_to_le16(usb_status);
291
292
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));
297
298 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma,
299 sizeof(*response_pkt), 1, 0);
300 return 0;
301}
302
303static int cdns3_ep0_feature_handle_device(struct cdns3_device *priv_dev,
304 struct usb_ctrlrequest *ctrl,
305 int set)
306{
307 enum usb_device_state state;
308 enum usb_device_speed speed;
309 int ret = 0;
310 u16 tmode;
311
312 state = priv_dev->gadget.state;
313 speed = priv_dev->gadget.speed;
314
315 switch (ctrl->wValue) {
316 case USB_DEVICE_REMOTE_WAKEUP:
317 priv_dev->wake_up_flag = !!set;
318 break;
319 case USB_DEVICE_U1_ENABLE:
320 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER)
321 return -EINVAL;
322
323 priv_dev->u1_allowed = !!set;
324 break;
325 case USB_DEVICE_U2_ENABLE:
326 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER)
327 return -EINVAL;
328
329 priv_dev->u2_allowed = !!set;
330 break;
331 case USB_DEVICE_LTM_ENABLE:
332 ret = -EINVAL;
333 break;
334 case USB_DEVICE_TEST_MODE:
335 if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
336 return -EINVAL;
337
338 tmode = le16_to_cpu(ctrl->wIndex);
339
340 if (!set || (tmode & 0xff) != 0)
341 return -EINVAL;
342
343 switch (tmode >> 8) {
344 case TEST_J:
345 case TEST_K:
346 case TEST_SE0_NAK:
347 case TEST_PACKET:
348 cdns3_ep0_complete_setup(priv_dev, 0, 1);
349
350
351
352
353
354 usleep_range(1000, 2000);
355 cdns3_set_register_bit(&priv_dev->regs->usb_cmd,
356 USB_CMD_STMODE |
357 USB_STS_TMODE_SEL(tmode - 1));
358 break;
359 default:
360 ret = -EINVAL;
361 }
362 break;
363 default:
364 ret = -EINVAL;
365 }
366
367 return ret;
368}
369
370static int cdns3_ep0_feature_handle_intf(struct cdns3_device *priv_dev,
371 struct usb_ctrlrequest *ctrl,
372 int set)
373{
374 u32 wValue;
375 int ret = 0;
376
377 wValue = le16_to_cpu(ctrl->wValue);
378
379 switch (wValue) {
380 case USB_INTRF_FUNC_SUSPEND:
381 break;
382 default:
383 ret = -EINVAL;
384 }
385
386 return ret;
387}
388
389static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev,
390 struct usb_ctrlrequest *ctrl,
391 int set)
392{
393 struct cdns3_endpoint *priv_ep;
394 int ret = 0;
395 u8 index;
396
397 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
398 return -EINVAL;
399
400 if (!(ctrl->wIndex & ~USB_DIR_IN))
401 return 0;
402
403 index = cdns3_ep_addr_to_index(ctrl->wIndex);
404 priv_ep = priv_dev->eps[index];
405
406 cdns3_select_ep(priv_dev, ctrl->wIndex);
407
408 if (set)
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);
412
413 cdns3_select_ep(priv_dev, 0x00);
414
415 return ret;
416}
417
418
419
420
421
422
423
424
425
426
427
428static int cdns3_req_ep0_handle_feature(struct cdns3_device *priv_dev,
429 struct usb_ctrlrequest *ctrl,
430 int set)
431{
432 int ret = 0;
433 u32 recip;
434
435 recip = ctrl->bRequestType & USB_RECIP_MASK;
436
437 switch (recip) {
438 case USB_RECIP_DEVICE:
439 ret = cdns3_ep0_feature_handle_device(priv_dev, ctrl, set);
440 break;
441 case USB_RECIP_INTERFACE:
442 ret = cdns3_ep0_feature_handle_intf(priv_dev, ctrl, set);
443 break;
444 case USB_RECIP_ENDPOINT:
445 ret = cdns3_ep0_feature_handle_endpoint(priv_dev, ctrl, set);
446 break;
447 default:
448 return -EINVAL;
449 }
450
451 return ret;
452}
453
454
455
456
457
458
459
460
461static int cdns3_req_ep0_set_sel(struct cdns3_device *priv_dev,
462 struct usb_ctrlrequest *ctrl_req)
463{
464 if (priv_dev->gadget.state < USB_STATE_ADDRESS)
465 return -EINVAL;
466
467 if (ctrl_req->wLength != 6) {
468 dev_err(priv_dev->dev, "Set SEL should be 6 bytes, got %d\n",
469 ctrl_req->wLength);
470 return -EINVAL;
471 }
472
473 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 6, 1, 0);
474 return 0;
475}
476
477
478
479
480
481
482
483
484
485static int cdns3_req_ep0_set_isoch_delay(struct cdns3_device *priv_dev,
486 struct usb_ctrlrequest *ctrl_req)
487{
488 if (ctrl_req->wIndex || ctrl_req->wLength)
489 return -EINVAL;
490
491 priv_dev->isoch_delay = ctrl_req->wValue;
492
493 return 0;
494}
495
496
497
498
499
500
501
502
503static int cdns3_ep0_standard_request(struct cdns3_device *priv_dev,
504 struct usb_ctrlrequest *ctrl_req)
505{
506 int ret;
507
508 switch (ctrl_req->bRequest) {
509 case USB_REQ_SET_ADDRESS:
510 ret = cdns3_req_ep0_set_address(priv_dev, ctrl_req);
511 break;
512 case USB_REQ_SET_CONFIGURATION:
513 ret = cdns3_req_ep0_set_configuration(priv_dev, ctrl_req);
514 break;
515 case USB_REQ_GET_STATUS:
516 ret = cdns3_req_ep0_get_status(priv_dev, ctrl_req);
517 break;
518 case USB_REQ_CLEAR_FEATURE:
519 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 0);
520 break;
521 case USB_REQ_SET_FEATURE:
522 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 1);
523 break;
524 case USB_REQ_SET_SEL:
525 ret = cdns3_req_ep0_set_sel(priv_dev, ctrl_req);
526 break;
527 case USB_REQ_SET_ISOCH_DELAY:
528 ret = cdns3_req_ep0_set_isoch_delay(priv_dev, ctrl_req);
529 break;
530 default:
531 ret = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
532 break;
533 }
534
535 return ret;
536}
537
538static void __pending_setup_status_handler(struct cdns3_device *priv_dev)
539{
540 struct usb_request *request = priv_dev->pending_status_request;
541
542 if (priv_dev->status_completion_no_call && request &&
543 request->complete) {
544 request->complete(&priv_dev->eps[0]->endpoint, request);
545 priv_dev->status_completion_no_call = 0;
546 }
547}
548
549void cdns3_pending_setup_status_handler(struct work_struct *work)
550{
551 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
552 pending_status_wq);
553 unsigned long flags;
554
555 spin_lock_irqsave(&priv_dev->lock, flags);
556 __pending_setup_status_handler(priv_dev);
557 spin_unlock_irqrestore(&priv_dev->lock, flags);
558}
559
560
561
562
563
564static void cdns3_ep0_setup_phase(struct cdns3_device *priv_dev)
565{
566 struct usb_ctrlrequest *ctrl = priv_dev->setup_buf;
567 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
568 int result;
569
570
571 invalidate_dcache_range(priv_dev->setup_dma,
572 priv_dev->setup_dma + ARCH_DMA_MINALIGN);
573
574 priv_dev->ep0_data_dir = ctrl->bRequestType & USB_DIR_IN;
575
576 trace_cdns3_ctrl_req(ctrl);
577
578 if (!list_empty(&priv_ep->pending_req_list)) {
579 struct usb_request *request;
580
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),
584 -ECONNRESET);
585 }
586
587 if (le16_to_cpu(ctrl->wLength))
588 priv_dev->ep0_stage = CDNS3_DATA_STAGE;
589 else
590 priv_dev->ep0_stage = CDNS3_STATUS_STAGE;
591
592 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
593 result = cdns3_ep0_standard_request(priv_dev, ctrl);
594 else
595 result = cdns3_ep0_delegate_req(priv_dev, ctrl);
596
597 if (result == USB_GADGET_DELAYED_STATUS)
598 return;
599
600 if (result < 0)
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);
604}
605
606static void cdns3_transfer_completed(struct cdns3_device *priv_dev)
607{
608 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
609
610 if (!list_empty(&priv_ep->pending_req_list)) {
611 struct usb_request *request;
612
613 trace_cdns3_complete_trb(priv_ep, priv_ep->trb_pool);
614 request = cdns3_next_request(&priv_ep->pending_req_list);
615
616
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));
621
622 request->actual =
623 TRB_LEN(le32_to_cpu(priv_ep->trb_pool->length));
624
625 priv_ep->dir = priv_dev->ep0_data_dir;
626 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 0);
627 }
628
629 cdns3_ep0_complete_setup(priv_dev, 0, 0);
630}
631
632
633
634
635
636
637
638static bool cdns3_check_new_setup(struct cdns3_device *priv_dev)
639{
640 u32 ep_sts_reg;
641
642 cdns3_select_ep(priv_dev, 0 | USB_DIR_OUT);
643 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
644
645 return !!(ep_sts_reg & (EP_STS_SETUP | EP_STS_STPWAIT));
646}
647
648
649
650
651
652
653void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir)
654{
655 u32 ep_sts_reg;
656
657 cdns3_select_ep(priv_dev, dir);
658
659 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
660 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
661
662 trace_cdns3_ep0_irq(priv_dev, ep_sts_reg);
663
664 __pending_setup_status_handler(priv_dev);
665
666 if (ep_sts_reg & EP_STS_SETUP)
667 priv_dev->wait_for_setup = 1;
668
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);
676 }
677
678 if (ep_sts_reg & EP_STS_DESCMIS) {
679 if (dir == 0 && !priv_dev->setup_pending)
680 cdns3_prepare_setup_packet(priv_dev);
681 }
682}
683
684
685
686
687
688
689static int cdns3_gadget_ep0_enable(struct usb_ep *ep,
690 const struct usb_endpoint_descriptor *desc)
691{
692 return -EINVAL;
693}
694
695
696
697
698
699
700static int cdns3_gadget_ep0_disable(struct usb_ep *ep)
701{
702 return -EINVAL;
703}
704
705
706
707
708
709
710
711
712static int cdns3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
713{
714
715 return 0;
716}
717
718
719
720
721
722
723
724
725
726static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
727 struct usb_request *request,
728 gfp_t gfp_flags)
729{
730 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
731 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
732 unsigned long flags;
733 int erdy_sent = 0;
734 int ret = 0;
735 u8 zlp = 0;
736
737 trace_cdns3_ep0_queue(priv_dev, request);
738
739
740 if (cdns3_check_new_setup(priv_dev))
741 return -ECONNRESET;
742
743
744 if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) {
745 spin_lock_irqsave(&priv_dev->lock, flags);
746 cdns3_select_ep(priv_dev, 0x00);
747
748 erdy_sent = !priv_dev->hw_configured_flag;
749 cdns3_set_hw_configuration(priv_dev);
750
751 if (!erdy_sent)
752 cdns3_ep0_complete_setup(priv_dev, 0, 1);
753
754 cdns3_allow_enable_l1(priv_dev, 1);
755
756 request->actual = 0;
757 priv_dev->status_completion_no_call = true;
758 priv_dev->pending_status_request = request;
759 spin_unlock_irqrestore(&priv_dev->lock, flags);
760
761
762
763
764
765
766#ifndef __UBOOT__
767 queue_work(system_freezable_wq, &priv_dev->pending_status_wq);
768#else
769 __pending_setup_status_handler(priv_dev);
770#endif
771 return 0;
772 }
773
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);
779 return -EBUSY;
780 }
781
782 ret = usb_gadget_map_request(&priv_dev->gadget, request,
783 priv_dev->ep0_data_dir);
784 if (ret) {
785 spin_unlock_irqrestore(&priv_dev->lock, flags);
786 dev_err(priv_dev->dev, "failed to map request\n");
787 return -EINVAL;
788 }
789
790 request->status = -EINPROGRESS;
791 list_add_tail(&request->list, &priv_ep->pending_req_list);
792
793 if (request->zero && request->length &&
794 (request->length % ep->maxpacket == 0))
795 zlp = 1;
796
797 cdns3_ep0_run_transfer(priv_dev, request->dma, request->length, 1, zlp);
798
799 spin_unlock_irqrestore(&priv_dev->lock, flags);
800
801 return ret;
802}
803
804
805
806
807
808
809
810int cdns3_gadget_ep_set_wedge(struct usb_ep *ep)
811{
812 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
813
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;
817
818 return 0;
819}
820
821const 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,
830};
831
832
833
834
835
836
837
838void cdns3_ep0_config(struct cdns3_device *priv_dev)
839{
840 struct cdns3_usb_regs __iomem *regs;
841 struct cdns3_endpoint *priv_ep;
842 u32 max_packet_size = 64;
843
844 regs = priv_dev->regs;
845
846 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
847 max_packet_size = 512;
848
849 priv_ep = priv_dev->eps[0];
850
851 if (!list_empty(&priv_ep->pending_req_list)) {
852 struct usb_request *request;
853
854 request = cdns3_next_request(&priv_ep->pending_req_list);
855 list_del_init(&request->list);
856 }
857
858 priv_dev->u1_allowed = 0;
859 priv_dev->u2_allowed = 0;
860
861 priv_dev->gadget.ep0->maxpacket = max_packet_size;
862 cdns3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(max_packet_size);
863
864
865 cdns3_select_ep(priv_dev, USB_DIR_OUT);
866
867 if (priv_dev->dev_ver >= DEV_VER_V3) {
868 cdns3_set_register_bit(&priv_dev->regs->dtrans,
869 BIT(0) | BIT(16));
870 cdns3_set_register_bit(&priv_dev->regs->tdl_from_trb,
871 BIT(0) | BIT(16));
872 }
873
874 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
875 ®s->ep_cfg);
876
877 writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN,
878 ®s->ep_sts_en);
879
880
881 cdns3_select_ep(priv_dev, USB_DIR_IN);
882
883 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
884 ®s->ep_cfg);
885
886 writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, ®s->ep_sts_en);
887
888 cdns3_set_register_bit(®s->usb_conf, USB_CONF_U1DS | USB_CONF_U2DS);
889}
890
891
892
893
894
895
896
897
898int cdns3_init_ep0(struct cdns3_device *priv_dev,
899 struct cdns3_endpoint *priv_ep)
900{
901 sprintf(priv_ep->name, "ep0");
902
903
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);
908#ifndef __UBOOT__
909 priv_ep->endpoint.address = 0;
910#endif
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;
918
919 return cdns3_allocate_trb_pool(priv_ep);
920}
921