1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#include <dm.h>
60#include <dm/device_compat.h>
61#include <dm/devres.h>
62#include <linux/bitops.h>
63#include <linux/delay.h>
64#include <linux/err.h>
65#include <linux/usb/gadget.h>
66#include <linux/compat.h>
67#include <linux/iopoll.h>
68#include <linux/dma-mapping.h>
69#include <linux/bitmap.h>
70#include <linux/bug.h>
71
72#include "core.h"
73#include "gadget-export.h"
74#include "gadget.h"
75#include "trace.h"
76#include "drd.h"
77
78#define readl_poll_timeout_atomic readl_poll_timeout
79#define usleep_range(a, b) udelay((b))
80
81static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
82 struct usb_request *request,
83 gfp_t gfp_flags);
84
85
86
87
88
89
90void cdns3_set_register_bit(void __iomem *ptr, u32 mask)
91{
92 mask = readl(ptr) | mask;
93 writel(mask, ptr);
94}
95
96
97
98
99
100
101
102u8 cdns3_ep_addr_to_index(u8 ep_addr)
103{
104 return (((ep_addr & 0x7F)) + ((ep_addr & USB_DIR_IN) ? 16 : 0));
105}
106
107static int cdns3_get_dma_pos(struct cdns3_device *priv_dev,
108 struct cdns3_endpoint *priv_ep)
109{
110 int dma_index;
111
112 dma_index = readl(&priv_dev->regs->ep_traddr) - priv_ep->trb_pool_dma;
113
114 return dma_index / TRB_SIZE;
115}
116
117
118
119
120
121
122
123struct usb_request *cdns3_next_request(struct list_head *list)
124{
125 return list_first_entry_or_null(list, struct usb_request, list);
126}
127
128
129
130
131
132
133
134struct cdns3_aligned_buf *cdns3_next_align_buf(struct list_head *list)
135{
136 return list_first_entry_or_null(list, struct cdns3_aligned_buf, list);
137}
138
139
140
141
142
143
144
145struct cdns3_request *cdns3_next_priv_request(struct list_head *list)
146{
147 return list_first_entry_or_null(list, struct cdns3_request, list);
148}
149
150
151
152
153
154
155void cdns3_select_ep(struct cdns3_device *priv_dev, u32 ep)
156{
157 if (priv_dev->selected_ep == ep)
158 return;
159
160 priv_dev->selected_ep = ep;
161 writel(ep, &priv_dev->regs->ep_sel);
162}
163
164dma_addr_t cdns3_trb_virt_to_dma(struct cdns3_endpoint *priv_ep,
165 struct cdns3_trb *trb)
166{
167 u32 offset = (char *)trb - (char *)priv_ep->trb_pool;
168
169 return priv_ep->trb_pool_dma + offset;
170}
171
172int cdns3_ring_size(struct cdns3_endpoint *priv_ep)
173{
174 switch (priv_ep->type) {
175 case USB_ENDPOINT_XFER_ISOC:
176 return TRB_ISO_RING_SIZE;
177 case USB_ENDPOINT_XFER_CONTROL:
178 return TRB_CTRL_RING_SIZE;
179 default:
180 return TRB_RING_SIZE;
181 }
182}
183
184
185
186
187
188
189
190int cdns3_allocate_trb_pool(struct cdns3_endpoint *priv_ep)
191{
192 int ring_size = cdns3_ring_size(priv_ep);
193 struct cdns3_trb *link_trb;
194
195 if (!priv_ep->trb_pool) {
196 priv_ep->trb_pool =
197 dma_alloc_coherent(ring_size,
198 (unsigned long *)&priv_ep->trb_pool_dma);
199 if (!priv_ep->trb_pool)
200 return -ENOMEM;
201 } else {
202 memset(priv_ep->trb_pool, 0, ring_size);
203 }
204
205 if (!priv_ep->num)
206 return 0;
207
208 priv_ep->num_trbs = ring_size / TRB_SIZE;
209
210 link_trb = (priv_ep->trb_pool + (priv_ep->num_trbs - 1));
211 link_trb->buffer = TRB_BUFFER(priv_ep->trb_pool_dma);
212 link_trb->control = TRB_CYCLE | TRB_TYPE(TRB_LINK) | TRB_TOGGLE;
213
214 return 0;
215}
216
217static void cdns3_free_trb_pool(struct cdns3_endpoint *priv_ep)
218{
219 if (priv_ep->trb_pool) {
220 dma_free_coherent(priv_ep->trb_pool);
221 priv_ep->trb_pool = NULL;
222 }
223}
224
225
226
227
228
229
230
231static void cdns3_ep_stall_flush(struct cdns3_endpoint *priv_ep)
232{
233 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
234 int val;
235
236 trace_cdns3_halt(priv_ep, 1, 1);
237
238 writel(EP_CMD_DFLUSH | EP_CMD_ERDY | EP_CMD_SSTALL,
239 &priv_dev->regs->ep_cmd);
240
241
242 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
243 !(val & EP_CMD_DFLUSH), 1000);
244 priv_ep->flags |= EP_STALLED;
245 priv_ep->flags &= ~EP_STALL_PENDING;
246}
247
248
249
250
251
252void cdns3_hw_reset_eps_config(struct cdns3_device *priv_dev)
253{
254 writel(USB_CONF_CFGRST, &priv_dev->regs->usb_conf);
255
256 cdns3_allow_enable_l1(priv_dev, 0);
257 priv_dev->hw_configured_flag = 0;
258 priv_dev->onchip_used_size = 0;
259 priv_dev->out_mem_is_allocated = 0;
260 priv_dev->wait_for_setup = 0;
261}
262
263
264
265
266
267
268
269
270
271
272
273
274static void cdns3_ep_inc_trb(int *index, u8 *cs, int trb_in_seg)
275{
276 (*index)++;
277 if (*index == (trb_in_seg - 1)) {
278 *index = 0;
279 *cs ^= 1;
280 }
281}
282
283
284
285
286
287static void cdns3_ep_inc_enq(struct cdns3_endpoint *priv_ep)
288{
289 priv_ep->free_trbs--;
290 cdns3_ep_inc_trb(&priv_ep->enqueue, &priv_ep->pcs, priv_ep->num_trbs);
291}
292
293
294
295
296
297static void cdns3_ep_inc_deq(struct cdns3_endpoint *priv_ep)
298{
299 priv_ep->free_trbs++;
300 cdns3_ep_inc_trb(&priv_ep->dequeue, &priv_ep->ccs, priv_ep->num_trbs);
301}
302
303void cdns3_move_deq_to_next_trb(struct cdns3_request *priv_req)
304{
305 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
306 int current_trb = priv_req->start_trb;
307
308 while (current_trb != priv_req->end_trb) {
309 cdns3_ep_inc_deq(priv_ep);
310 current_trb = priv_ep->dequeue;
311 }
312
313 cdns3_ep_inc_deq(priv_ep);
314}
315
316
317
318
319
320
321
322
323
324
325
326void cdns3_allow_enable_l1(struct cdns3_device *priv_dev, int enable)
327{
328 if (enable)
329 writel(USB_CONF_L1EN, &priv_dev->regs->usb_conf);
330 else
331 writel(USB_CONF_L1DS, &priv_dev->regs->usb_conf);
332}
333
334enum usb_device_speed cdns3_get_speed(struct cdns3_device *priv_dev)
335{
336 u32 reg;
337
338 reg = readl(&priv_dev->regs->usb_sts);
339
340 if (DEV_SUPERSPEED(reg))
341 return USB_SPEED_SUPER;
342 else if (DEV_HIGHSPEED(reg))
343 return USB_SPEED_HIGH;
344 else if (DEV_FULLSPEED(reg))
345 return USB_SPEED_FULL;
346 else if (DEV_LOWSPEED(reg))
347 return USB_SPEED_LOW;
348 return USB_SPEED_UNKNOWN;
349}
350
351
352
353
354
355
356
357
358
359static int cdns3_start_all_request(struct cdns3_device *priv_dev,
360 struct cdns3_endpoint *priv_ep)
361{
362 struct usb_request *request;
363 int ret = 0;
364
365 while (!list_empty(&priv_ep->deferred_req_list)) {
366 request = cdns3_next_request(&priv_ep->deferred_req_list);
367
368 ret = cdns3_ep_run_transfer(priv_ep, request);
369 if (ret)
370 return ret;
371
372 list_del(&request->list);
373 list_add_tail(&request->list,
374 &priv_ep->pending_req_list);
375 }
376
377 priv_ep->flags &= ~EP_RING_FULL;
378 return ret;
379}
380
381
382
383
384
385
386
387#define cdns3_wa2_enable_detection(priv_dev, ep_priv, reg) do { \
388 if (!priv_ep->dir && priv_ep->type != USB_ENDPOINT_XFER_ISOC) { \
389 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_DET; \
390 (reg) |= EP_STS_EN_DESCMISEN; \
391 } } while (0)
392
393
394
395
396
397
398
399static void cdns3_wa2_descmiss_copy_data(struct cdns3_endpoint *priv_ep,
400 struct usb_request *request)
401{
402 struct usb_request *descmiss_req;
403 struct cdns3_request *descmiss_priv_req;
404
405 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
406 int chunk_end;
407 int length;
408
409 descmiss_priv_req =
410 cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
411 descmiss_req = &descmiss_priv_req->request;
412
413
414 if (descmiss_priv_req->flags & REQUEST_PENDING)
415 break;
416
417 chunk_end = descmiss_priv_req->flags & REQUEST_INTERNAL_CH;
418 length = request->actual + descmiss_req->actual;
419
420 request->status = descmiss_req->status;
421
422 if (length <= request->length) {
423 memcpy(&((u8 *)request->buf)[request->actual],
424 descmiss_req->buf,
425 descmiss_req->actual);
426 request->actual = length;
427 } else {
428
429 request->status = -ENOMEM;
430 }
431
432 list_del_init(&descmiss_priv_req->list);
433
434 kfree(descmiss_req->buf);
435 cdns3_gadget_ep_free_request(&priv_ep->endpoint, descmiss_req);
436 --priv_ep->wa2_counter;
437
438 if (!chunk_end)
439 break;
440 }
441}
442
443struct usb_request *cdns3_wa2_gadget_giveback(struct cdns3_device *priv_dev,
444 struct cdns3_endpoint *priv_ep,
445 struct cdns3_request *priv_req)
446{
447 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN &&
448 priv_req->flags & REQUEST_INTERNAL) {
449 struct usb_request *req;
450
451 req = cdns3_next_request(&priv_ep->deferred_req_list);
452
453 priv_ep->descmis_req = NULL;
454
455 if (!req)
456 return NULL;
457
458 cdns3_wa2_descmiss_copy_data(priv_ep, req);
459 if (!(priv_ep->flags & EP_QUIRK_END_TRANSFER) &&
460 req->length != req->actual) {
461
462 return NULL;
463 }
464
465 if (req->status == -EINPROGRESS)
466 req->status = 0;
467
468 list_del_init(&req->list);
469 cdns3_start_all_request(priv_dev, priv_ep);
470 return req;
471 }
472
473 return &priv_req->request;
474}
475
476int cdns3_wa2_gadget_ep_queue(struct cdns3_device *priv_dev,
477 struct cdns3_endpoint *priv_ep,
478 struct cdns3_request *priv_req)
479{
480 int deferred = 0;
481
482
483
484
485
486
487 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
488 u32 reg;
489
490 cdns3_select_ep(priv_dev, priv_ep->num | priv_ep->dir);
491 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
492 reg = readl(&priv_dev->regs->ep_sts_en);
493 reg &= ~EP_STS_EN_DESCMISEN;
494 trace_cdns3_wa2(priv_ep, "workaround disabled\n");
495 writel(reg, &priv_dev->regs->ep_sts_en);
496 }
497
498 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
499 u8 pending_empty = list_empty(&priv_ep->pending_req_list);
500 u8 descmiss_empty = list_empty(&priv_ep->wa2_descmiss_req_list);
501
502
503
504
505
506
507 if (pending_empty && !descmiss_empty &&
508 !(priv_req->flags & REQUEST_INTERNAL)) {
509 cdns3_wa2_descmiss_copy_data(priv_ep,
510 &priv_req->request);
511
512 trace_cdns3_wa2(priv_ep, "get internal stored data");
513
514 list_add_tail(&priv_req->request.list,
515 &priv_ep->pending_req_list);
516 cdns3_gadget_giveback(priv_ep, priv_req,
517 priv_req->request.status);
518
519
520
521
522
523
524 return EINPROGRESS;
525 }
526
527
528
529
530
531 if (!pending_empty && !descmiss_empty) {
532 trace_cdns3_wa2(priv_ep, "wait for pending transfer\n");
533 deferred = 1;
534 }
535
536 if (priv_req->flags & REQUEST_INTERNAL)
537 list_add_tail(&priv_req->list,
538 &priv_ep->wa2_descmiss_req_list);
539 }
540
541 return deferred;
542}
543
544static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep)
545{
546 struct cdns3_request *priv_req;
547
548 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
549 u8 chain;
550
551 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
552 chain = !!(priv_req->flags & REQUEST_INTERNAL_CH);
553
554 trace_cdns3_wa2(priv_ep, "removes eldest request");
555
556 kfree(priv_req->request.buf);
557 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
558 &priv_req->request);
559 list_del_init(&priv_req->list);
560 --priv_ep->wa2_counter;
561
562 if (!chain)
563 break;
564 }
565}
566
567
568
569
570
571
572
573
574static void cdns3_wa2_descmissing_packet(struct cdns3_endpoint *priv_ep)
575{
576 struct cdns3_request *priv_req;
577 struct usb_request *request;
578
579 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_DET) {
580 priv_ep->flags &= ~EP_QUIRK_EXTRA_BUF_DET;
581 priv_ep->flags |= EP_QUIRK_EXTRA_BUF_EN;
582 }
583
584 trace_cdns3_wa2(priv_ep, "Description Missing detected\n");
585
586 if (priv_ep->wa2_counter >= CDNS3_WA2_NUM_BUFFERS)
587 cdns3_wa2_remove_old_request(priv_ep);
588
589 request = cdns3_gadget_ep_alloc_request(&priv_ep->endpoint,
590 GFP_ATOMIC);
591 if (!request)
592 goto err;
593
594 priv_req = to_cdns3_request(request);
595 priv_req->flags |= REQUEST_INTERNAL;
596
597
598
599
600
601
602
603 if (priv_ep->descmis_req)
604 priv_ep->descmis_req->flags |= REQUEST_INTERNAL_CH;
605
606 priv_req->request.buf = kzalloc(CDNS3_DESCMIS_BUF_SIZE,
607 GFP_ATOMIC);
608 priv_ep->wa2_counter++;
609
610 if (!priv_req->request.buf) {
611 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
612 goto err;
613 }
614
615 priv_req->request.length = CDNS3_DESCMIS_BUF_SIZE;
616 priv_ep->descmis_req = priv_req;
617
618 __cdns3_gadget_ep_queue(&priv_ep->endpoint,
619 &priv_ep->descmis_req->request,
620 GFP_ATOMIC);
621
622 return;
623
624err:
625 dev_err(priv_ep->cdns3_dev->dev,
626 "Failed: No sufficient memory for DESCMIS\n");
627}
628
629
630
631
632
633
634
635
636
637
638
639void cdns3_gadget_giveback(struct cdns3_endpoint *priv_ep,
640 struct cdns3_request *priv_req,
641 int status)
642{
643 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
644 struct usb_request *request = &priv_req->request;
645
646 list_del_init(&request->list);
647
648 if (request->status == -EINPROGRESS)
649 request->status = status;
650
651 usb_gadget_unmap_request(&priv_dev->gadget, request,
652 priv_ep->dir);
653
654 if ((priv_req->flags & REQUEST_UNALIGNED) &&
655 priv_ep->dir == USB_DIR_OUT && !request->status)
656 memcpy(request->buf, priv_req->aligned_buf->buf,
657 request->length);
658
659 priv_req->flags &= ~(REQUEST_PENDING | REQUEST_UNALIGNED);
660 trace_cdns3_gadget_giveback(priv_req);
661
662 if (priv_dev->dev_ver < DEV_VER_V2) {
663 request = cdns3_wa2_gadget_giveback(priv_dev, priv_ep,
664 priv_req);
665 if (!request)
666 return;
667 }
668
669 if (request->complete) {
670 spin_unlock(&priv_dev->lock);
671 usb_gadget_giveback_request(&priv_ep->endpoint,
672 request);
673 spin_lock(&priv_dev->lock);
674 }
675
676 if (request->buf == priv_dev->zlp_buf)
677 cdns3_gadget_ep_free_request(&priv_ep->endpoint, request);
678}
679
680void cdns3_wa1_restore_cycle_bit(struct cdns3_endpoint *priv_ep)
681{
682
683 if (priv_ep->wa1_set) {
684 trace_cdns3_wa1(priv_ep, "restore cycle bit");
685
686 priv_ep->wa1_set = 0;
687 priv_ep->wa1_trb_index = 0xFFFF;
688 if (priv_ep->wa1_cycle_bit) {
689 priv_ep->wa1_trb->control =
690 priv_ep->wa1_trb->control | 0x1;
691 } else {
692 priv_ep->wa1_trb->control =
693 priv_ep->wa1_trb->control & ~0x1;
694 }
695 }
696}
697
698static void cdns3_free_aligned_request_buf(struct cdns3_device *priv_dev)
699{
700 struct cdns3_aligned_buf *buf, *tmp;
701 unsigned long flags;
702
703 spin_lock_irqsave(&priv_dev->lock, flags);
704
705 list_for_each_entry_safe(buf, tmp, &priv_dev->aligned_buf_list, list) {
706 if (!buf->in_use) {
707 list_del(&buf->list);
708
709
710
711
712
713
714 spin_unlock_irqrestore(&priv_dev->lock, flags);
715 dma_free_coherent(buf->buf);
716 kfree(buf);
717 spin_lock_irqsave(&priv_dev->lock, flags);
718 }
719 }
720
721 spin_unlock_irqrestore(&priv_dev->lock, flags);
722}
723
724static int cdns3_prepare_aligned_request_buf(struct cdns3_request *priv_req)
725{
726 struct cdns3_endpoint *priv_ep = priv_req->priv_ep;
727 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
728 struct cdns3_aligned_buf *buf;
729
730
731 if (!((uintptr_t)priv_req->request.buf & 0x7))
732 return 0;
733
734 buf = priv_req->aligned_buf;
735
736 if (!buf || priv_req->request.length > buf->size) {
737 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
738 if (!buf)
739 return -ENOMEM;
740
741 buf->size = priv_req->request.length;
742
743 buf->buf = dma_alloc_coherent(buf->size,
744 (unsigned long *)&buf->dma);
745 if (!buf->buf) {
746 kfree(buf);
747 return -ENOMEM;
748 }
749
750 if (priv_req->aligned_buf) {
751 trace_cdns3_free_aligned_request(priv_req);
752 priv_req->aligned_buf->in_use = 0;
753#ifndef __UBOOT__
754 queue_work(system_freezable_wq,
755 &priv_dev->aligned_buf_wq);
756#else
757 cdns3_free_aligned_request_buf(priv_dev);
758#endif
759 }
760
761 buf->in_use = 1;
762 priv_req->aligned_buf = buf;
763
764 list_add_tail(&buf->list,
765 &priv_dev->aligned_buf_list);
766 }
767
768 if (priv_ep->dir == USB_DIR_IN) {
769 memcpy(buf->buf, priv_req->request.buf,
770 priv_req->request.length);
771 }
772
773 priv_req->flags |= REQUEST_UNALIGNED;
774 trace_cdns3_prepare_aligned_request(priv_req);
775
776 return 0;
777}
778
779static int cdns3_wa1_update_guard(struct cdns3_endpoint *priv_ep,
780 struct cdns3_trb *trb)
781{
782 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
783
784 if (!priv_ep->wa1_set) {
785 u32 doorbell;
786
787 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
788
789 if (doorbell) {
790 priv_ep->wa1_cycle_bit = priv_ep->pcs ? TRB_CYCLE : 0;
791 priv_ep->wa1_set = 1;
792 priv_ep->wa1_trb = trb;
793 priv_ep->wa1_trb_index = priv_ep->enqueue;
794 trace_cdns3_wa1(priv_ep, "set guard");
795 return 0;
796 }
797 }
798 return 1;
799}
800
801static void cdns3_wa1_tray_restore_cycle_bit(struct cdns3_device *priv_dev,
802 struct cdns3_endpoint *priv_ep)
803{
804 int dma_index;
805 u32 doorbell;
806
807 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
808 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
809
810 if (!doorbell || dma_index != priv_ep->wa1_trb_index)
811 cdns3_wa1_restore_cycle_bit(priv_ep);
812}
813
814
815
816
817
818
819
820int cdns3_ep_run_transfer(struct cdns3_endpoint *priv_ep,
821 struct usb_request *request)
822{
823 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
824 struct cdns3_request *priv_req;
825 struct cdns3_trb *trb;
826 dma_addr_t trb_dma;
827 u32 togle_pcs = 1;
828 int sg_iter = 0;
829 int num_trb = 1;
830 int address;
831 u32 control;
832 int pcs;
833
834 if (num_trb > priv_ep->free_trbs) {
835 priv_ep->flags |= EP_RING_FULL;
836 return -ENOBUFS;
837 }
838
839 priv_req = to_cdns3_request(request);
840 address = priv_ep->endpoint.desc->bEndpointAddress;
841
842 priv_ep->flags |= EP_PENDING_REQUEST;
843
844
845 if (priv_req->flags & REQUEST_UNALIGNED)
846 trb_dma = priv_req->aligned_buf->dma;
847 else
848 trb_dma = request->dma;
849
850 trb = priv_ep->trb_pool + priv_ep->enqueue;
851 priv_req->start_trb = priv_ep->enqueue;
852 priv_req->trb = trb;
853
854 cdns3_select_ep(priv_ep->cdns3_dev, address);
855
856
857 if ((priv_ep->enqueue + num_trb) >= (priv_ep->num_trbs - 1)) {
858 struct cdns3_trb *link_trb;
859 int doorbell, dma_index;
860 u32 ch_bit = 0;
861
862 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
863 dma_index = cdns3_get_dma_pos(priv_dev, priv_ep);
864
865
866 if (doorbell && dma_index == priv_ep->num_trbs - 1) {
867 priv_ep->flags |= EP_DEFERRED_DRDY;
868 return -ENOBUFS;
869 }
870
871
872 link_trb = priv_ep->trb_pool + (priv_ep->num_trbs - 1);
873
874
875
876
877
878
879
880
881 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC ||
882 TRBS_PER_SEGMENT > 2)
883 ch_bit = TRB_CHAIN;
884
885 link_trb->control = ((priv_ep->pcs) ? TRB_CYCLE : 0) |
886 TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit;
887 }
888
889 if (priv_dev->dev_ver <= DEV_VER_V2)
890 togle_pcs = cdns3_wa1_update_guard(priv_ep, trb);
891
892
893 control = priv_ep->pcs ? 0 : TRB_CYCLE;
894
895 do {
896 u32 length;
897 u16 td_size = 0;
898
899
900 control |= TRB_TYPE(TRB_NORMAL);
901 trb->buffer = TRB_BUFFER(trb_dma);
902
903 length = request->length;
904
905 if (likely(priv_dev->dev_ver >= DEV_VER_V2))
906 td_size = DIV_ROUND_UP(length,
907 priv_ep->endpoint.maxpacket);
908
909 trb->length = TRB_BURST_LEN(priv_ep->trb_burst_size) |
910 TRB_LEN(length);
911 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
912 trb->length |= TRB_TDL_SS_SIZE(td_size);
913 else
914 control |= TRB_TDL_HS_SIZE(td_size);
915
916 pcs = priv_ep->pcs ? TRB_CYCLE : 0;
917
918
919
920
921
922 if (sg_iter != 0)
923 control |= pcs;
924
925 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir) {
926 control |= TRB_IOC | TRB_ISP;
927 } else {
928
929 if (sg_iter == (num_trb - 1) && sg_iter != 0)
930 control |= pcs | TRB_IOC | TRB_ISP;
931 }
932
933 if (sg_iter)
934 trb->control = control;
935 else
936 priv_req->trb->control = control;
937
938 control = 0;
939 ++sg_iter;
940 priv_req->end_trb = priv_ep->enqueue;
941 cdns3_ep_inc_enq(priv_ep);
942 trb = priv_ep->trb_pool + priv_ep->enqueue;
943 } while (sg_iter < num_trb);
944
945 trb = priv_req->trb;
946
947 priv_req->flags |= REQUEST_PENDING;
948
949 if (sg_iter == 1)
950 trb->control |= TRB_IOC | TRB_ISP;
951
952
953
954
955 dmb();
956
957
958 if (togle_pcs)
959 trb->control = trb->control ^ 1;
960
961 if (priv_dev->dev_ver <= DEV_VER_V2)
962 cdns3_wa1_tray_restore_cycle_bit(priv_dev, priv_ep);
963
964 trace_cdns3_prepare_trb(priv_ep, priv_req->trb);
965
966
967
968
969
970 dmb();
971
972
973
974
975
976 if (priv_ep->flags & EP_UPDATE_EP_TRBADDR) {
977
978
979
980
981
982 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir &&
983 !(priv_ep->flags & EP_QUIRK_ISO_OUT_EN)) {
984 priv_ep->flags |= EP_QUIRK_ISO_OUT_EN;
985 cdns3_set_register_bit(&priv_dev->regs->ep_cfg,
986 EP_CFG_ENABLE);
987 }
988
989 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma +
990 priv_req->start_trb * TRB_SIZE),
991 &priv_dev->regs->ep_traddr);
992
993 priv_ep->flags &= ~EP_UPDATE_EP_TRBADDR;
994 }
995
996 if (!priv_ep->wa1_set && !(priv_ep->flags & EP_STALLED)) {
997 trace_cdns3_ring(priv_ep);
998
999 writel(EP_STS_TRBERR | EP_STS_DESCMIS, &priv_dev->regs->ep_sts);
1000 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1001 trace_cdns3_doorbell_epx(priv_ep->name,
1002 readl(&priv_dev->regs->ep_traddr));
1003 }
1004
1005
1006 __cdns3_gadget_wakeup(priv_dev);
1007
1008 return 0;
1009}
1010
1011void cdns3_set_hw_configuration(struct cdns3_device *priv_dev)
1012{
1013 struct cdns3_endpoint *priv_ep;
1014 struct usb_ep *ep;
1015 int val;
1016
1017 if (priv_dev->hw_configured_flag)
1018 return;
1019
1020 writel(USB_CONF_CFGSET, &priv_dev->regs->usb_conf);
1021 writel(EP_CMD_ERDY | EP_CMD_REQ_CMPL, &priv_dev->regs->ep_cmd);
1022
1023 cdns3_set_register_bit(&priv_dev->regs->usb_conf,
1024 USB_CONF_U1EN | USB_CONF_U2EN);
1025
1026
1027 readl_poll_timeout_atomic(&priv_dev->regs->usb_sts, val,
1028 val & USB_STS_CFGSTS_MASK, 100);
1029
1030 priv_dev->hw_configured_flag = 1;
1031
1032 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
1033 priv_ep = ep_to_cdns3_ep(ep);
1034 if (priv_ep->flags & EP_ENABLED)
1035 cdns3_start_all_request(priv_dev, priv_ep);
1036 }
1037}
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082static bool cdns3_request_handled(struct cdns3_endpoint *priv_ep,
1083 struct cdns3_request *priv_req)
1084{
1085 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1086 struct cdns3_trb *trb = priv_req->trb;
1087 int current_index = 0;
1088 int handled = 0;
1089 int doorbell;
1090
1091 current_index = cdns3_get_dma_pos(priv_dev, priv_ep);
1092 doorbell = !!(readl(&priv_dev->regs->ep_cmd) & EP_CMD_DRDY);
1093
1094 trb = &priv_ep->trb_pool[priv_req->start_trb];
1095
1096 if ((trb->control & TRB_CYCLE) != priv_ep->ccs)
1097 goto finish;
1098
1099 if (doorbell == 1 && current_index == priv_ep->dequeue)
1100 goto finish;
1101
1102
1103 if (TRBS_PER_SEGMENT == 2 && priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1104 handled = 1;
1105 goto finish;
1106 }
1107
1108 if (priv_ep->enqueue == priv_ep->dequeue &&
1109 priv_ep->free_trbs == 0) {
1110 handled = 1;
1111 } else if (priv_ep->dequeue < current_index) {
1112 if ((current_index == (priv_ep->num_trbs - 1)) &&
1113 !priv_ep->dequeue)
1114 goto finish;
1115
1116 if (priv_req->end_trb >= priv_ep->dequeue &&
1117 priv_req->end_trb < current_index)
1118 handled = 1;
1119 } else if (priv_ep->dequeue > current_index) {
1120 if (priv_req->end_trb < current_index ||
1121 priv_req->end_trb >= priv_ep->dequeue)
1122 handled = 1;
1123 }
1124
1125finish:
1126 trace_cdns3_request_handled(priv_req, current_index, handled);
1127
1128 return handled;
1129}
1130
1131static void cdns3_transfer_completed(struct cdns3_device *priv_dev,
1132 struct cdns3_endpoint *priv_ep)
1133{
1134 struct cdns3_request *priv_req;
1135 struct usb_request *request;
1136 struct cdns3_trb *trb;
1137
1138 while (!list_empty(&priv_ep->pending_req_list)) {
1139 request = cdns3_next_request(&priv_ep->pending_req_list);
1140 priv_req = to_cdns3_request(request);
1141
1142
1143
1144
1145#ifndef __UBOOT__
1146 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1147#else
1148 cdns3_select_ep(priv_dev,
1149 priv_ep->endpoint.desc->bEndpointAddress);
1150#endif
1151
1152 if (!cdns3_request_handled(priv_ep, priv_req))
1153 goto prepare_next_td;
1154
1155 trb = priv_ep->trb_pool + priv_ep->dequeue;
1156 trace_cdns3_complete_trb(priv_ep, trb);
1157
1158 if (trb != priv_req->trb)
1159 dev_warn(priv_dev->dev,
1160 "request_trb=0x%p, queue_trb=0x%p\n",
1161 priv_req->trb, trb);
1162
1163 request->actual = TRB_LEN(le32_to_cpu(trb->length));
1164 cdns3_move_deq_to_next_trb(priv_req);
1165 cdns3_gadget_giveback(priv_ep, priv_req, 0);
1166
1167 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC &&
1168 TRBS_PER_SEGMENT == 2)
1169 break;
1170 }
1171 priv_ep->flags &= ~EP_PENDING_REQUEST;
1172
1173prepare_next_td:
1174 if (!(priv_ep->flags & EP_STALLED) &&
1175 !(priv_ep->flags & EP_STALL_PENDING))
1176 cdns3_start_all_request(priv_dev, priv_ep);
1177}
1178
1179void cdns3_rearm_transfer(struct cdns3_endpoint *priv_ep, u8 rearm)
1180{
1181 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1182
1183 cdns3_wa1_restore_cycle_bit(priv_ep);
1184
1185 if (rearm) {
1186 trace_cdns3_ring(priv_ep);
1187
1188
1189 dmb();
1190 writel(EP_CMD_DRDY, &priv_dev->regs->ep_cmd);
1191
1192 __cdns3_gadget_wakeup(priv_dev);
1193
1194 trace_cdns3_doorbell_epx(priv_ep->name,
1195 readl(&priv_dev->regs->ep_traddr));
1196 }
1197}
1198
1199
1200
1201
1202
1203
1204
1205static int cdns3_check_ep_interrupt_proceed(struct cdns3_endpoint *priv_ep)
1206{
1207 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1208 u32 ep_sts_reg;
1209
1210#ifndef __UBOOT__
1211 cdns3_select_ep(priv_dev, priv_ep->endpoint.address);
1212#else
1213 cdns3_select_ep(priv_dev, priv_ep->endpoint.desc->bEndpointAddress);
1214#endif
1215
1216 trace_cdns3_epx_irq(priv_dev, priv_ep);
1217
1218 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
1219 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
1220
1221 if (ep_sts_reg & EP_STS_TRBERR) {
1222 if (priv_ep->flags & EP_STALL_PENDING &&
1223 !(ep_sts_reg & EP_STS_DESCMIS &&
1224 priv_dev->dev_ver < DEV_VER_V2)) {
1225 cdns3_ep_stall_flush(priv_ep);
1226 }
1227
1228
1229
1230
1231
1232
1233
1234
1235 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC &&
1236 !priv_ep->wa1_set) {
1237 if (!priv_ep->dir) {
1238 u32 ep_cfg = readl(&priv_dev->regs->ep_cfg);
1239
1240 ep_cfg &= ~EP_CFG_ENABLE;
1241 writel(ep_cfg, &priv_dev->regs->ep_cfg);
1242 priv_ep->flags &= ~EP_QUIRK_ISO_OUT_EN;
1243 }
1244 cdns3_transfer_completed(priv_dev, priv_ep);
1245 } else if (!(priv_ep->flags & EP_STALLED) &&
1246 !(priv_ep->flags & EP_STALL_PENDING)) {
1247 if (priv_ep->flags & EP_DEFERRED_DRDY) {
1248 priv_ep->flags &= ~EP_DEFERRED_DRDY;
1249 cdns3_start_all_request(priv_dev, priv_ep);
1250 } else {
1251 cdns3_rearm_transfer(priv_ep,
1252 priv_ep->wa1_set);
1253 }
1254 }
1255 }
1256
1257 if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP)) {
1258 if (priv_ep->flags & EP_QUIRK_EXTRA_BUF_EN) {
1259 if (ep_sts_reg & EP_STS_ISP)
1260 priv_ep->flags |= EP_QUIRK_END_TRANSFER;
1261 else
1262 priv_ep->flags &= ~EP_QUIRK_END_TRANSFER;
1263 }
1264
1265 cdns3_transfer_completed(priv_dev, priv_ep);
1266 }
1267
1268
1269
1270
1271
1272
1273
1274 if (ep_sts_reg & EP_STS_DESCMIS && priv_dev->dev_ver < DEV_VER_V2 &&
1275 !(priv_ep->flags & EP_STALLED))
1276 cdns3_wa2_descmissing_packet(priv_ep);
1277
1278 return 0;
1279}
1280
1281static void cdns3_disconnect_gadget(struct cdns3_device *priv_dev)
1282{
1283 if (priv_dev->gadget_driver && priv_dev->gadget_driver->disconnect) {
1284 spin_unlock(&priv_dev->lock);
1285 priv_dev->gadget_driver->disconnect(&priv_dev->gadget);
1286 spin_lock(&priv_dev->lock);
1287 }
1288}
1289
1290
1291
1292
1293
1294
1295
1296static void cdns3_check_usb_interrupt_proceed(struct cdns3_device *priv_dev,
1297 u32 usb_ists)
1298{
1299 int speed = 0;
1300
1301 trace_cdns3_usb_irq(priv_dev, usb_ists);
1302 if (usb_ists & USB_ISTS_L1ENTI) {
1303
1304
1305
1306
1307
1308 if (readl(&priv_dev->regs->drbl))
1309 __cdns3_gadget_wakeup(priv_dev);
1310 }
1311
1312
1313 if (usb_ists & (USB_ISTS_CON2I | USB_ISTS_CONI)) {
1314 speed = cdns3_get_speed(priv_dev);
1315 priv_dev->gadget.speed = speed;
1316 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_POWERED);
1317 cdns3_ep0_config(priv_dev);
1318 }
1319
1320
1321 if (usb_ists & (USB_ISTS_DIS2I | USB_ISTS_DISI)) {
1322 cdns3_disconnect_gadget(priv_dev);
1323 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
1324 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
1325 cdns3_hw_reset_eps_config(priv_dev);
1326 }
1327
1328 if (usb_ists & (USB_ISTS_L2ENTI | USB_ISTS_U3ENTI)) {
1329 if (priv_dev->gadget_driver &&
1330 priv_dev->gadget_driver->suspend) {
1331 spin_unlock(&priv_dev->lock);
1332 priv_dev->gadget_driver->suspend(&priv_dev->gadget);
1333 spin_lock(&priv_dev->lock);
1334 }
1335 }
1336
1337 if (usb_ists & (USB_ISTS_L2EXTI | USB_ISTS_U3EXTI)) {
1338 if (priv_dev->gadget_driver &&
1339 priv_dev->gadget_driver->resume) {
1340 spin_unlock(&priv_dev->lock);
1341 priv_dev->gadget_driver->resume(&priv_dev->gadget);
1342 spin_lock(&priv_dev->lock);
1343 }
1344 }
1345
1346
1347 if (usb_ists & (USB_ISTS_UWRESI | USB_ISTS_UHRESI | USB_ISTS_U2RESI)) {
1348 if (priv_dev->gadget_driver) {
1349 spin_unlock(&priv_dev->lock);
1350 usb_gadget_udc_reset(&priv_dev->gadget,
1351 priv_dev->gadget_driver);
1352 spin_lock(&priv_dev->lock);
1353
1354
1355 speed = cdns3_get_speed(priv_dev);
1356 priv_dev->gadget.speed = speed;
1357 cdns3_hw_reset_eps_config(priv_dev);
1358 cdns3_ep0_config(priv_dev);
1359 }
1360 }
1361}
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371static irqreturn_t cdns3_device_irq_handler(int irq, void *data)
1372{
1373 struct cdns3_device *priv_dev;
1374 struct cdns3 *cdns = data;
1375 irqreturn_t ret = IRQ_NONE;
1376 u32 reg;
1377
1378 priv_dev = cdns->gadget_dev;
1379
1380
1381 reg = readl(&priv_dev->regs->usb_ists);
1382 if (reg) {
1383
1384
1385
1386
1387
1388
1389
1390 reg = ~reg & readl(&priv_dev->regs->usb_ien);
1391
1392 writel(reg, &priv_dev->regs->usb_ien);
1393 ret = IRQ_WAKE_THREAD;
1394 }
1395
1396
1397 reg = readl(&priv_dev->regs->ep_ists);
1398 if (reg) {
1399 writel(0, &priv_dev->regs->ep_ien);
1400 ret = IRQ_WAKE_THREAD;
1401 }
1402
1403 return ret;
1404}
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415static irqreturn_t cdns3_device_thread_irq_handler(int irq, void *data)
1416{
1417 struct cdns3_device *priv_dev;
1418 struct cdns3 *cdns = data;
1419 irqreturn_t ret = IRQ_NONE;
1420 unsigned long flags;
1421 int bit;
1422 u32 reg;
1423
1424 priv_dev = cdns->gadget_dev;
1425 spin_lock_irqsave(&priv_dev->lock, flags);
1426
1427 reg = readl(&priv_dev->regs->usb_ists);
1428 if (reg) {
1429 writel(reg, &priv_dev->regs->usb_ists);
1430 writel(USB_IEN_INIT, &priv_dev->regs->usb_ien);
1431 cdns3_check_usb_interrupt_proceed(priv_dev, reg);
1432 ret = IRQ_HANDLED;
1433 }
1434
1435 reg = readl(&priv_dev->regs->ep_ists);
1436
1437
1438 if (reg & EP_ISTS_EP_OUT0) {
1439 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_OUT);
1440 ret = IRQ_HANDLED;
1441 }
1442
1443
1444 if (reg & EP_ISTS_EP_IN0) {
1445 cdns3_check_ep0_interrupt_proceed(priv_dev, USB_DIR_IN);
1446 ret = IRQ_HANDLED;
1447 }
1448
1449
1450 reg &= ~(EP_ISTS_EP_OUT0 | EP_ISTS_EP_IN0);
1451 if (!reg)
1452 goto irqend;
1453
1454 for_each_set_bit(bit, (unsigned long *)®,
1455 sizeof(u32) * BITS_PER_BYTE) {
1456 cdns3_check_ep_interrupt_proceed(priv_dev->eps[bit]);
1457 ret = IRQ_HANDLED;
1458 }
1459
1460irqend:
1461 writel(~0, &priv_dev->regs->ep_ien);
1462 spin_unlock_irqrestore(&priv_dev->lock, flags);
1463
1464 return ret;
1465}
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479static int cdns3_ep_onchip_buffer_reserve(struct cdns3_device *priv_dev,
1480 int size, int is_in)
1481{
1482 int remained;
1483
1484
1485 remained = priv_dev->onchip_buffers - priv_dev->onchip_used_size - 2;
1486
1487 if (is_in) {
1488 if (remained < size)
1489 return -EPERM;
1490
1491 priv_dev->onchip_used_size += size;
1492 } else {
1493 int required;
1494
1495
1496
1497
1498
1499 if (priv_dev->out_mem_is_allocated >= size)
1500 return 0;
1501
1502 required = size - priv_dev->out_mem_is_allocated;
1503
1504 if (required > remained)
1505 return -EPERM;
1506
1507 priv_dev->out_mem_is_allocated += required;
1508 priv_dev->onchip_used_size += required;
1509 }
1510
1511 return 0;
1512}
1513
1514void cdns3_configure_dmult(struct cdns3_device *priv_dev,
1515 struct cdns3_endpoint *priv_ep)
1516{
1517 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
1518
1519
1520 if (priv_dev->dev_ver <= DEV_VER_V2)
1521 writel(USB_CONF_DMULT, ®s->usb_conf);
1522
1523 if (priv_dev->dev_ver == DEV_VER_V2)
1524 writel(USB_CONF2_EN_TDL_TRB, ®s->usb_conf2);
1525
1526 if (priv_dev->dev_ver >= DEV_VER_V3 && priv_ep) {
1527 u32 mask;
1528
1529 if (priv_ep->dir)
1530 mask = BIT(priv_ep->num + 16);
1531 else
1532 mask = BIT(priv_ep->num);
1533
1534 if (priv_ep->type != USB_ENDPOINT_XFER_ISOC) {
1535 cdns3_set_register_bit(®s->tdl_from_trb, mask);
1536 cdns3_set_register_bit(®s->tdl_beh, mask);
1537 cdns3_set_register_bit(®s->tdl_beh2, mask);
1538 cdns3_set_register_bit(®s->dma_adv_td, mask);
1539 }
1540
1541 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
1542 cdns3_set_register_bit(®s->tdl_from_trb, mask);
1543
1544 cdns3_set_register_bit(®s->dtrans, mask);
1545 }
1546}
1547
1548
1549
1550
1551
1552void cdns3_ep_config(struct cdns3_endpoint *priv_ep)
1553{
1554 bool is_iso_ep = (priv_ep->type == USB_ENDPOINT_XFER_ISOC);
1555 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1556 u32 bEndpointAddress = priv_ep->num | priv_ep->dir;
1557 u32 max_packet_size = 0;
1558 u8 maxburst = 0;
1559 u32 ep_cfg = 0;
1560 u8 buffering;
1561 u8 mult = 0;
1562 int ret;
1563
1564 buffering = CDNS3_EP_BUF_SIZE - 1;
1565
1566 cdns3_configure_dmult(priv_dev, priv_ep);
1567
1568 switch (priv_ep->type) {
1569 case USB_ENDPOINT_XFER_INT:
1570 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_INT);
1571
1572 if ((priv_dev->dev_ver == DEV_VER_V2 && !priv_ep->dir) ||
1573 priv_dev->dev_ver > DEV_VER_V2)
1574 ep_cfg |= EP_CFG_TDL_CHK;
1575 break;
1576 case USB_ENDPOINT_XFER_BULK:
1577 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_BULK);
1578
1579 if ((priv_dev->dev_ver == DEV_VER_V2 && !priv_ep->dir) ||
1580 priv_dev->dev_ver > DEV_VER_V2)
1581 ep_cfg |= EP_CFG_TDL_CHK;
1582 break;
1583 default:
1584 ep_cfg = EP_CFG_EPTYPE(USB_ENDPOINT_XFER_ISOC);
1585 mult = CDNS3_EP_ISO_HS_MULT - 1;
1586 buffering = mult + 1;
1587 }
1588
1589 switch (priv_dev->gadget.speed) {
1590 case USB_SPEED_FULL:
1591 max_packet_size = is_iso_ep ? 1023 : 64;
1592 break;
1593 case USB_SPEED_HIGH:
1594 max_packet_size = is_iso_ep ? 1024 : 512;
1595 break;
1596 case USB_SPEED_SUPER:
1597
1598 mult = 0;
1599 max_packet_size = 1024;
1600 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
1601 maxburst = CDNS3_EP_ISO_SS_BURST - 1;
1602 buffering = (mult + 1) *
1603 (maxburst + 1);
1604
1605 if (priv_ep->interval > 1)
1606 buffering++;
1607 } else {
1608 maxburst = CDNS3_EP_BUF_SIZE - 1;
1609 }
1610 break;
1611 default:
1612
1613 return;
1614 }
1615
1616 if (max_packet_size == 1024)
1617 priv_ep->trb_burst_size = 128;
1618 else if (max_packet_size >= 512)
1619 priv_ep->trb_burst_size = 64;
1620 else
1621 priv_ep->trb_burst_size = 16;
1622
1623 ret = cdns3_ep_onchip_buffer_reserve(priv_dev, buffering + 1,
1624 !!priv_ep->dir);
1625 if (ret) {
1626 dev_err(priv_dev->dev, "onchip mem is full, ep is invalid\n");
1627 return;
1628 }
1629
1630 ep_cfg |= EP_CFG_MAXPKTSIZE(max_packet_size) |
1631 EP_CFG_MULT(mult) |
1632 EP_CFG_BUFFERING(buffering) |
1633 EP_CFG_MAXBURST(maxburst);
1634
1635 cdns3_select_ep(priv_dev, bEndpointAddress);
1636 writel(ep_cfg, &priv_dev->regs->ep_cfg);
1637
1638 dev_dbg(priv_dev->dev, "Configure %s: with val %08x\n",
1639 priv_ep->name, ep_cfg);
1640}
1641
1642
1643static int cdns3_ep_dir_is_correct(struct usb_endpoint_descriptor *desc,
1644 struct cdns3_endpoint *priv_ep)
1645{
1646 return (priv_ep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) ||
1647 (priv_ep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc));
1648}
1649
1650static struct
1651cdns3_endpoint *cdns3_find_available_ep(struct cdns3_device *priv_dev,
1652 struct usb_endpoint_descriptor *desc)
1653{
1654 struct usb_ep *ep;
1655 struct cdns3_endpoint *priv_ep;
1656
1657 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
1658 unsigned long num;
1659
1660 char c[2] = {ep->name[2], '\0'};
1661
1662 num = dectoul(c, NULL);
1663
1664 priv_ep = ep_to_cdns3_ep(ep);
1665 if (cdns3_ep_dir_is_correct(desc, priv_ep)) {
1666 if (!(priv_ep->flags & EP_CLAIMED)) {
1667 priv_ep->num = num;
1668 return priv_ep;
1669 }
1670 }
1671 }
1672
1673 return ERR_PTR(-ENOENT);
1674}
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692static struct
1693usb_ep *cdns3_gadget_match_ep(struct usb_gadget *gadget,
1694 struct usb_endpoint_descriptor *desc,
1695 struct usb_ss_ep_comp_descriptor *comp_desc)
1696{
1697 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
1698 struct cdns3_endpoint *priv_ep;
1699 unsigned long flags;
1700
1701 priv_ep = cdns3_find_available_ep(priv_dev, desc);
1702 if (IS_ERR(priv_ep)) {
1703 dev_err(priv_dev->dev, "no available ep\n");
1704 return NULL;
1705 }
1706
1707 dev_dbg(priv_dev->dev, "match endpoint: %s\n", priv_ep->name);
1708
1709 spin_lock_irqsave(&priv_dev->lock, flags);
1710 priv_ep->endpoint.desc = desc;
1711 priv_ep->dir = usb_endpoint_dir_in(desc) ? USB_DIR_IN : USB_DIR_OUT;
1712 priv_ep->type = usb_endpoint_type(desc);
1713 priv_ep->flags |= EP_CLAIMED;
1714 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
1715
1716 spin_unlock_irqrestore(&priv_dev->lock, flags);
1717 return &priv_ep->endpoint;
1718}
1719
1720
1721
1722
1723
1724
1725
1726
1727struct usb_request *cdns3_gadget_ep_alloc_request(struct usb_ep *ep,
1728 gfp_t gfp_flags)
1729{
1730 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
1731 struct cdns3_request *priv_req;
1732
1733 priv_req = kzalloc(sizeof(*priv_req), gfp_flags);
1734 if (!priv_req)
1735 return NULL;
1736
1737 priv_req->priv_ep = priv_ep;
1738
1739 trace_cdns3_alloc_request(priv_req);
1740 return &priv_req->request;
1741}
1742
1743
1744
1745
1746
1747
1748void cdns3_gadget_ep_free_request(struct usb_ep *ep,
1749 struct usb_request *request)
1750{
1751 struct cdns3_request *priv_req = to_cdns3_request(request);
1752
1753 if (priv_req->aligned_buf)
1754 priv_req->aligned_buf->in_use = 0;
1755
1756 trace_cdns3_free_request(priv_req);
1757 kfree(priv_req);
1758}
1759
1760
1761
1762
1763
1764
1765
1766
1767static int cdns3_gadget_ep_enable(struct usb_ep *ep,
1768 const struct usb_endpoint_descriptor *desc)
1769{
1770 struct cdns3_endpoint *priv_ep;
1771 struct cdns3_device *priv_dev;
1772 u32 reg = EP_STS_EN_TRBERREN;
1773 u32 bEndpointAddress;
1774 unsigned long flags;
1775 int enable = 1;
1776 int ret;
1777 int val;
1778
1779 priv_ep = ep_to_cdns3_ep(ep);
1780 priv_dev = priv_ep->cdns3_dev;
1781
1782 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
1783 dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
1784 return -EINVAL;
1785 }
1786
1787 if (!desc->wMaxPacketSize) {
1788 dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
1789 return -EINVAL;
1790 }
1791
1792 if (WARN_ON(priv_ep->flags & EP_ENABLED))
1793 return 0;
1794
1795 spin_lock_irqsave(&priv_dev->lock, flags);
1796
1797 priv_ep->endpoint.desc = desc;
1798 priv_ep->type = usb_endpoint_type(desc);
1799 priv_ep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0;
1800
1801 if (priv_ep->interval > ISO_MAX_INTERVAL &&
1802 priv_ep->type == USB_ENDPOINT_XFER_ISOC) {
1803 dev_err(priv_dev->dev, "Driver is limited to %d period\n",
1804 ISO_MAX_INTERVAL);
1805
1806 ret = -EINVAL;
1807 goto exit;
1808 }
1809
1810 ret = cdns3_allocate_trb_pool(priv_ep);
1811
1812 if (ret)
1813 goto exit;
1814
1815 bEndpointAddress = priv_ep->num | priv_ep->dir;
1816 cdns3_select_ep(priv_dev, bEndpointAddress);
1817
1818 trace_cdns3_gadget_ep_enable(priv_ep);
1819
1820 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
1821
1822 ret = readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
1823 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
1824 1000);
1825
1826 if (unlikely(ret)) {
1827 cdns3_free_trb_pool(priv_ep);
1828 ret = -EINVAL;
1829 goto exit;
1830 }
1831
1832
1833 cdns3_set_register_bit(&priv_dev->regs->ep_ien,
1834 BIT(cdns3_ep_addr_to_index(bEndpointAddress)));
1835
1836 if (priv_dev->dev_ver < DEV_VER_V2)
1837 cdns3_wa2_enable_detection(priv_dev, priv_ep, reg);
1838
1839 writel(reg, &priv_dev->regs->ep_sts_en);
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849 if (priv_ep->type == USB_ENDPOINT_XFER_ISOC && !priv_ep->dir)
1850 enable = 0;
1851
1852 if (enable)
1853 cdns3_set_register_bit(&priv_dev->regs->ep_cfg, EP_CFG_ENABLE);
1854
1855 ep->desc = desc;
1856 priv_ep->flags &= ~(EP_PENDING_REQUEST | EP_STALLED | EP_STALL_PENDING |
1857 EP_QUIRK_ISO_OUT_EN | EP_QUIRK_EXTRA_BUF_EN);
1858 priv_ep->flags |= EP_ENABLED | EP_UPDATE_EP_TRBADDR;
1859 priv_ep->wa1_set = 0;
1860 priv_ep->enqueue = 0;
1861 priv_ep->dequeue = 0;
1862 reg = readl(&priv_dev->regs->ep_sts);
1863 priv_ep->pcs = !!EP_STS_CCS(reg);
1864 priv_ep->ccs = !!EP_STS_CCS(reg);
1865
1866 priv_ep->free_trbs = priv_ep->num_trbs - 1;
1867exit:
1868 spin_unlock_irqrestore(&priv_dev->lock, flags);
1869
1870 return ret;
1871}
1872
1873
1874
1875
1876
1877
1878
1879static int cdns3_gadget_ep_disable(struct usb_ep *ep)
1880{
1881 struct cdns3_endpoint *priv_ep;
1882 struct cdns3_request *priv_req;
1883 struct cdns3_device *priv_dev;
1884 struct usb_request *request;
1885 unsigned long flags;
1886 int ret = 0;
1887 u32 ep_cfg;
1888 int val;
1889
1890 if (!ep) {
1891 pr_err("usbss: invalid parameters\n");
1892 return -EINVAL;
1893 }
1894
1895 priv_ep = ep_to_cdns3_ep(ep);
1896 priv_dev = priv_ep->cdns3_dev;
1897
1898 if (WARN_ON(!(priv_ep->flags & EP_ENABLED)))
1899 return 0;
1900
1901 spin_lock_irqsave(&priv_dev->lock, flags);
1902
1903 trace_cdns3_gadget_ep_disable(priv_ep);
1904
1905 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
1906
1907 ep_cfg = readl(&priv_dev->regs->ep_cfg);
1908 ep_cfg &= ~EP_CFG_ENABLE;
1909 writel(ep_cfg, &priv_dev->regs->ep_cfg);
1910
1911
1912
1913
1914
1915
1916 readl_poll_timeout_atomic(&priv_dev->regs->ep_sts, val,
1917 !(val & EP_STS_DBUSY), 10);
1918 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
1919
1920 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
1921 !(val & (EP_CMD_CSTALL | EP_CMD_EPRST)),
1922 1000);
1923 if (unlikely(ret))
1924 dev_err(priv_dev->dev, "Timeout: %s resetting failed.\n",
1925 priv_ep->name);
1926
1927 while (!list_empty(&priv_ep->pending_req_list)) {
1928 request = cdns3_next_request(&priv_ep->pending_req_list);
1929
1930 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
1931 -ESHUTDOWN);
1932 }
1933
1934 while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
1935 priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
1936
1937 kfree(priv_req->request.buf);
1938 cdns3_gadget_ep_free_request(&priv_ep->endpoint,
1939 &priv_req->request);
1940 list_del_init(&priv_req->list);
1941 --priv_ep->wa2_counter;
1942 }
1943
1944 while (!list_empty(&priv_ep->deferred_req_list)) {
1945 request = cdns3_next_request(&priv_ep->deferred_req_list);
1946
1947 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
1948 -ESHUTDOWN);
1949 }
1950
1951 priv_ep->descmis_req = NULL;
1952
1953 ep->desc = NULL;
1954 priv_ep->flags &= ~EP_ENABLED;
1955
1956 spin_unlock_irqrestore(&priv_dev->lock, flags);
1957
1958 return ret;
1959}
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969static int __cdns3_gadget_ep_queue(struct usb_ep *ep,
1970 struct usb_request *request,
1971 gfp_t gfp_flags)
1972{
1973 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
1974 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
1975 struct cdns3_request *priv_req;
1976 int ret = 0;
1977
1978 request->actual = 0;
1979 request->status = -EINPROGRESS;
1980 priv_req = to_cdns3_request(request);
1981 trace_cdns3_ep_queue(priv_req);
1982
1983 if (priv_dev->dev_ver < DEV_VER_V2) {
1984 ret = cdns3_wa2_gadget_ep_queue(priv_dev, priv_ep,
1985 priv_req);
1986
1987 if (ret == EINPROGRESS)
1988 return 0;
1989 }
1990
1991 ret = cdns3_prepare_aligned_request_buf(priv_req);
1992 if (ret < 0)
1993 return ret;
1994
1995 ret = usb_gadget_map_request(&priv_dev->gadget, request,
1996 usb_endpoint_dir_in(ep->desc));
1997 if (ret)
1998 return ret;
1999
2000 list_add_tail(&request->list, &priv_ep->deferred_req_list);
2001
2002
2003
2004
2005
2006
2007 if (priv_dev->hw_configured_flag && !(priv_ep->flags & EP_STALLED) &&
2008 !(priv_ep->flags & EP_STALL_PENDING))
2009 cdns3_start_all_request(priv_dev, priv_ep);
2010
2011 return 0;
2012}
2013
2014static int cdns3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
2015 gfp_t gfp_flags)
2016{
2017 struct usb_request *zlp_request;
2018 struct cdns3_endpoint *priv_ep;
2019 struct cdns3_device *priv_dev;
2020 unsigned long flags;
2021 int ret;
2022
2023 if (!request || !ep)
2024 return -EINVAL;
2025
2026 priv_ep = ep_to_cdns3_ep(ep);
2027 priv_dev = priv_ep->cdns3_dev;
2028
2029 spin_lock_irqsave(&priv_dev->lock, flags);
2030
2031 ret = __cdns3_gadget_ep_queue(ep, request, gfp_flags);
2032
2033 if (ret == 0 && request->zero && request->length &&
2034 (request->length % ep->maxpacket == 0)) {
2035 struct cdns3_request *priv_req;
2036
2037 zlp_request = cdns3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
2038 zlp_request->buf = priv_dev->zlp_buf;
2039 zlp_request->length = 0;
2040
2041 priv_req = to_cdns3_request(zlp_request);
2042 priv_req->flags |= REQUEST_ZLP;
2043
2044 dev_dbg(priv_dev->dev, "Queuing ZLP for endpoint: %s\n",
2045 priv_ep->name);
2046 ret = __cdns3_gadget_ep_queue(ep, zlp_request, gfp_flags);
2047 }
2048
2049 spin_unlock_irqrestore(&priv_dev->lock, flags);
2050 return ret;
2051}
2052
2053
2054
2055
2056
2057
2058
2059
2060int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
2061 struct usb_request *request)
2062{
2063 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2064 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2065 struct usb_request *req, *req_temp;
2066 struct cdns3_request *priv_req;
2067 struct cdns3_trb *link_trb;
2068 unsigned long flags;
2069 int ret = 0;
2070
2071 if (!ep || !request || !ep->desc)
2072 return -EINVAL;
2073
2074 spin_lock_irqsave(&priv_dev->lock, flags);
2075
2076 priv_req = to_cdns3_request(request);
2077
2078 trace_cdns3_ep_dequeue(priv_req);
2079
2080 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2081
2082 list_for_each_entry_safe(req, req_temp, &priv_ep->pending_req_list,
2083 list) {
2084 if (request == req)
2085 goto found;
2086 }
2087
2088 list_for_each_entry_safe(req, req_temp, &priv_ep->deferred_req_list,
2089 list) {
2090 if (request == req)
2091 goto found;
2092 }
2093
2094 goto not_found;
2095
2096found:
2097
2098 if (priv_ep->wa1_trb == priv_req->trb)
2099 cdns3_wa1_restore_cycle_bit(priv_ep);
2100
2101 link_trb = priv_req->trb;
2102 cdns3_move_deq_to_next_trb(priv_req);
2103 cdns3_gadget_giveback(priv_ep, priv_req, -ECONNRESET);
2104
2105
2106 request = cdns3_next_request(&priv_ep->deferred_req_list);
2107 if (request) {
2108 priv_req = to_cdns3_request(request);
2109
2110 link_trb->buffer = TRB_BUFFER(priv_ep->trb_pool_dma +
2111 (priv_req->start_trb * TRB_SIZE));
2112 link_trb->control = (link_trb->control & TRB_CYCLE) |
2113 TRB_TYPE(TRB_LINK) | TRB_CHAIN | TRB_TOGGLE;
2114 } else {
2115 priv_ep->flags |= EP_UPDATE_EP_TRBADDR;
2116 }
2117
2118not_found:
2119 spin_unlock_irqrestore(&priv_dev->lock, flags);
2120 return ret;
2121}
2122
2123
2124
2125
2126
2127
2128void __cdns3_gadget_ep_set_halt(struct cdns3_endpoint *priv_ep)
2129{
2130 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2131
2132 trace_cdns3_halt(priv_ep, 1, 0);
2133
2134 if (!(priv_ep->flags & EP_STALLED)) {
2135 u32 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
2136
2137 if (!(ep_sts_reg & EP_STS_DBUSY))
2138 cdns3_ep_stall_flush(priv_ep);
2139 else
2140 priv_ep->flags |= EP_STALL_PENDING;
2141 }
2142}
2143
2144
2145
2146
2147
2148
2149int __cdns3_gadget_ep_clear_halt(struct cdns3_endpoint *priv_ep)
2150{
2151 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2152 struct usb_request *request;
2153 int ret = 0;
2154 int val;
2155
2156 trace_cdns3_halt(priv_ep, 0, 0);
2157
2158 writel(EP_CMD_CSTALL | EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2159
2160
2161 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2162 !(val & EP_CMD_EPRST), 100);
2163 if (ret)
2164 return -EINVAL;
2165
2166 priv_ep->flags &= ~(EP_STALLED | EP_STALL_PENDING);
2167
2168 request = cdns3_next_request(&priv_ep->pending_req_list);
2169
2170 if (request)
2171 cdns3_rearm_transfer(priv_ep, 1);
2172
2173 cdns3_start_all_request(priv_dev, priv_ep);
2174 return ret;
2175}
2176
2177
2178
2179
2180
2181
2182
2183
2184int cdns3_gadget_ep_set_halt(struct usb_ep *ep, int value)
2185{
2186 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
2187 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
2188 unsigned long flags;
2189 int ret = 0;
2190
2191 if (!(priv_ep->flags & EP_ENABLED))
2192 return -EPERM;
2193
2194 spin_lock_irqsave(&priv_dev->lock, flags);
2195
2196 cdns3_select_ep(priv_dev, ep->desc->bEndpointAddress);
2197
2198 if (!value) {
2199 priv_ep->flags &= ~EP_WEDGE;
2200 ret = __cdns3_gadget_ep_clear_halt(priv_ep);
2201 } else {
2202 __cdns3_gadget_ep_set_halt(priv_ep);
2203 }
2204
2205 spin_unlock_irqrestore(&priv_dev->lock, flags);
2206
2207 return ret;
2208}
2209
2210extern const struct usb_ep_ops cdns3_gadget_ep0_ops;
2211
2212static const struct usb_ep_ops cdns3_gadget_ep_ops = {
2213 .enable = cdns3_gadget_ep_enable,
2214 .disable = cdns3_gadget_ep_disable,
2215 .alloc_request = cdns3_gadget_ep_alloc_request,
2216 .free_request = cdns3_gadget_ep_free_request,
2217 .queue = cdns3_gadget_ep_queue,
2218 .dequeue = cdns3_gadget_ep_dequeue,
2219 .set_halt = cdns3_gadget_ep_set_halt,
2220 .set_wedge = cdns3_gadget_ep_set_wedge,
2221};
2222
2223
2224
2225
2226
2227
2228
2229static int cdns3_gadget_get_frame(struct usb_gadget *gadget)
2230{
2231 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2232
2233 return readl(&priv_dev->regs->usb_itpn);
2234}
2235
2236int __cdns3_gadget_wakeup(struct cdns3_device *priv_dev)
2237{
2238 enum usb_device_speed speed;
2239
2240 speed = cdns3_get_speed(priv_dev);
2241
2242 if (speed >= USB_SPEED_SUPER)
2243 return 0;
2244
2245
2246 writel(USB_CONF_LGO_L0, &priv_dev->regs->usb_conf);
2247
2248 return 0;
2249}
2250
2251static int cdns3_gadget_wakeup(struct usb_gadget *gadget)
2252{
2253 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2254 unsigned long flags;
2255 int ret = 0;
2256
2257 spin_lock_irqsave(&priv_dev->lock, flags);
2258 ret = __cdns3_gadget_wakeup(priv_dev);
2259 spin_unlock_irqrestore(&priv_dev->lock, flags);
2260 return ret;
2261}
2262
2263static int cdns3_gadget_set_selfpowered(struct usb_gadget *gadget,
2264 int is_selfpowered)
2265{
2266 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2267 unsigned long flags;
2268
2269 spin_lock_irqsave(&priv_dev->lock, flags);
2270 priv_dev->is_selfpowered = !!is_selfpowered;
2271 spin_unlock_irqrestore(&priv_dev->lock, flags);
2272 return 0;
2273}
2274
2275static int cdns3_gadget_pullup(struct usb_gadget *gadget, int is_on)
2276{
2277 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2278
2279 if (is_on)
2280 writel(USB_CONF_DEVEN, &priv_dev->regs->usb_conf);
2281 else
2282 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
2283
2284 return 0;
2285}
2286
2287static void cdns3_gadget_config(struct cdns3_device *priv_dev)
2288{
2289 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
2290 u32 reg;
2291
2292 cdns3_ep0_config(priv_dev);
2293
2294
2295 writel(EP_IEN_EP_OUT0 | EP_IEN_EP_IN0, ®s->ep_ien);
2296
2297
2298
2299
2300
2301 if (priv_dev->dev_ver == DEV_VER_TI_V1) {
2302 reg = readl(®s->dbg_link1);
2303
2304 reg &= ~DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_MASK;
2305 reg |= DBG_LINK1_LFPS_MIN_GEN_U1_EXIT(0x55) |
2306 DBG_LINK1_LFPS_MIN_GEN_U1_EXIT_SET;
2307 writel(reg, ®s->dbg_link1);
2308 }
2309
2310
2311
2312
2313
2314
2315 reg = readl(®s->dma_axi_ctrl);
2316 reg |= DMA_AXI_CTRL_MARPROT(DMA_AXI_CTRL_NON_SECURE) |
2317 DMA_AXI_CTRL_MAWPROT(DMA_AXI_CTRL_NON_SECURE);
2318 writel(reg, ®s->dma_axi_ctrl);
2319
2320
2321 writel(USB_IEN_INIT, ®s->usb_ien);
2322 writel(USB_CONF_CLK2OFFDS | USB_CONF_L1DS, ®s->usb_conf);
2323
2324 cdns3_configure_dmult(priv_dev, NULL);
2325
2326 cdns3_gadget_pullup(&priv_dev->gadget, 1);
2327}
2328
2329
2330
2331
2332
2333
2334
2335
2336static int cdns3_gadget_udc_start(struct usb_gadget *gadget,
2337 struct usb_gadget_driver *driver)
2338{
2339 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2340 unsigned long flags;
2341
2342 spin_lock_irqsave(&priv_dev->lock, flags);
2343 priv_dev->gadget_driver = driver;
2344 cdns3_gadget_config(priv_dev);
2345 spin_unlock_irqrestore(&priv_dev->lock, flags);
2346 return 0;
2347}
2348
2349
2350
2351
2352
2353
2354
2355static int cdns3_gadget_udc_stop(struct usb_gadget *gadget)
2356{
2357 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2358 struct cdns3_endpoint *priv_ep;
2359 u32 bEndpointAddress;
2360 struct usb_ep *ep;
2361 int ret = 0;
2362 int val;
2363
2364 priv_dev->gadget_driver = NULL;
2365
2366 priv_dev->onchip_used_size = 0;
2367 priv_dev->out_mem_is_allocated = 0;
2368 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
2369
2370 list_for_each_entry(ep, &priv_dev->gadget.ep_list, ep_list) {
2371 priv_ep = ep_to_cdns3_ep(ep);
2372 bEndpointAddress = priv_ep->num | priv_ep->dir;
2373 cdns3_select_ep(priv_dev, bEndpointAddress);
2374 writel(EP_CMD_EPRST, &priv_dev->regs->ep_cmd);
2375 readl_poll_timeout_atomic(&priv_dev->regs->ep_cmd, val,
2376 !(val & EP_CMD_EPRST), 100);
2377 }
2378
2379
2380 writel(0, &priv_dev->regs->usb_ien);
2381 writel(USB_CONF_DEVDS, &priv_dev->regs->usb_conf);
2382
2383 return ret;
2384}
2385
2386static void cdns3_gadget_udc_set_speed(struct usb_gadget *gadget,
2387 enum usb_device_speed speed)
2388{
2389 struct cdns3_device *priv_dev = gadget_to_cdns3_device(gadget);
2390
2391 switch (speed) {
2392 case USB_SPEED_FULL:
2393 writel(USB_CONF_SFORCE_FS, &priv_dev->regs->usb_conf);
2394 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2395 break;
2396 case USB_SPEED_HIGH:
2397 writel(USB_CONF_USB3DIS, &priv_dev->regs->usb_conf);
2398 break;
2399 case USB_SPEED_SUPER:
2400 break;
2401 default:
2402 dev_err(priv_dev->dev, "invalid speed parameter %d\n", speed);
2403 }
2404
2405 priv_dev->gadget.speed = speed;
2406}
2407
2408static const struct usb_gadget_ops cdns3_gadget_ops = {
2409 .get_frame = cdns3_gadget_get_frame,
2410 .wakeup = cdns3_gadget_wakeup,
2411 .set_selfpowered = cdns3_gadget_set_selfpowered,
2412 .pullup = cdns3_gadget_pullup,
2413 .udc_start = cdns3_gadget_udc_start,
2414 .udc_stop = cdns3_gadget_udc_stop,
2415 .match_ep = cdns3_gadget_match_ep,
2416 .udc_set_speed = cdns3_gadget_udc_set_speed,
2417};
2418
2419static void cdns3_free_all_eps(struct cdns3_device *priv_dev)
2420{
2421 int i;
2422
2423
2424 priv_dev->eps[16] = NULL;
2425
2426 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++)
2427 if (priv_dev->eps[i]) {
2428 cdns3_free_trb_pool(priv_dev->eps[i]);
2429 devm_kfree(priv_dev->dev, priv_dev->eps[i]);
2430 }
2431}
2432
2433
2434
2435
2436
2437
2438
2439static int cdns3_init_eps(struct cdns3_device *priv_dev)
2440{
2441 u32 ep_enabled_reg, iso_ep_reg;
2442 struct cdns3_endpoint *priv_ep;
2443 int ep_dir, ep_number;
2444 u32 ep_mask;
2445 int ret = 0;
2446 int i;
2447
2448
2449 ep_enabled_reg = readl(&priv_dev->regs->usb_cap3);
2450 iso_ep_reg = readl(&priv_dev->regs->usb_cap4);
2451
2452 dev_dbg(priv_dev->dev, "Initializing non-zero endpoints\n");
2453
2454 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
2455 ep_dir = i >> 4;
2456 ep_number = i & 0xF;
2457 ep_mask = BIT(i);
2458
2459 if (!(ep_enabled_reg & ep_mask))
2460 continue;
2461
2462 if (ep_dir && !ep_number) {
2463 priv_dev->eps[i] = priv_dev->eps[0];
2464 continue;
2465 }
2466
2467 priv_ep = devm_kzalloc(priv_dev->dev, sizeof(*priv_ep),
2468 GFP_KERNEL);
2469 if (!priv_ep) {
2470 ret = -ENOMEM;
2471 goto err;
2472 }
2473
2474
2475 priv_ep->cdns3_dev = priv_dev;
2476 priv_dev->eps[i] = priv_ep;
2477 priv_ep->num = ep_number;
2478 priv_ep->dir = ep_dir ? USB_DIR_IN : USB_DIR_OUT;
2479
2480 if (!ep_number) {
2481 ret = cdns3_init_ep0(priv_dev, priv_ep);
2482 if (ret) {
2483 dev_err(priv_dev->dev, "Failed to init ep0\n");
2484 goto err;
2485 }
2486 } else {
2487 snprintf(priv_ep->name, sizeof(priv_ep->name), "ep%d%s",
2488 ep_number, !!ep_dir ? "in" : "out");
2489 priv_ep->endpoint.name = priv_ep->name;
2490
2491 usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
2492 CDNS3_EP_MAX_PACKET_LIMIT);
2493 priv_ep->endpoint.max_streams = CDNS3_EP_MAX_STREAMS;
2494 priv_ep->endpoint.ops = &cdns3_gadget_ep_ops;
2495 if (ep_dir)
2496 priv_ep->endpoint.caps.dir_in = 1;
2497 else
2498 priv_ep->endpoint.caps.dir_out = 1;
2499
2500 if (iso_ep_reg & ep_mask)
2501 priv_ep->endpoint.caps.type_iso = 1;
2502
2503 priv_ep->endpoint.caps.type_bulk = 1;
2504 priv_ep->endpoint.caps.type_int = 1;
2505
2506 list_add_tail(&priv_ep->endpoint.ep_list,
2507 &priv_dev->gadget.ep_list);
2508 }
2509
2510 priv_ep->flags = 0;
2511
2512 dev_info(priv_dev->dev, "Initialized %s support: %s %s\n",
2513 priv_ep->name,
2514 priv_ep->endpoint.caps.type_bulk ? "BULK, INT" : "",
2515 priv_ep->endpoint.caps.type_iso ? "ISO" : "");
2516
2517 INIT_LIST_HEAD(&priv_ep->pending_req_list);
2518 INIT_LIST_HEAD(&priv_ep->deferred_req_list);
2519 INIT_LIST_HEAD(&priv_ep->wa2_descmiss_req_list);
2520 }
2521
2522 return 0;
2523err:
2524 cdns3_free_all_eps(priv_dev);
2525 return -ENOMEM;
2526}
2527
2528void cdns3_gadget_exit(struct cdns3 *cdns)
2529{
2530 struct cdns3_device *priv_dev;
2531
2532 priv_dev = cdns->gadget_dev;
2533
2534 usb_del_gadget_udc(&priv_dev->gadget);
2535
2536 cdns3_free_all_eps(priv_dev);
2537
2538 while (!list_empty(&priv_dev->aligned_buf_list)) {
2539 struct cdns3_aligned_buf *buf;
2540
2541 buf = cdns3_next_align_buf(&priv_dev->aligned_buf_list);
2542 dma_free_coherent(buf->buf);
2543
2544 list_del(&buf->list);
2545 kfree(buf);
2546 }
2547
2548 dma_free_coherent(priv_dev->setup_buf);
2549
2550 kfree(priv_dev->zlp_buf);
2551 kfree(priv_dev);
2552 cdns->gadget_dev = NULL;
2553 cdns3_drd_switch_gadget(cdns, 0);
2554}
2555
2556static int cdns3_gadget_start(struct cdns3 *cdns)
2557{
2558 struct cdns3_device *priv_dev;
2559 u32 max_speed;
2560 int ret;
2561
2562 priv_dev = kzalloc(sizeof(*priv_dev), GFP_KERNEL);
2563 if (!priv_dev)
2564 return -ENOMEM;
2565
2566 cdns->gadget_dev = priv_dev;
2567 priv_dev->sysdev = cdns->dev;
2568 priv_dev->dev = cdns->dev;
2569 priv_dev->regs = cdns->dev_regs;
2570
2571 dev_read_u32(priv_dev->dev, "cdns,on-chip-buff-size",
2572 &priv_dev->onchip_buffers);
2573
2574 if (priv_dev->onchip_buffers <= 0) {
2575 u32 reg = readl(&priv_dev->regs->usb_cap2);
2576
2577 priv_dev->onchip_buffers = USB_CAP2_ACTUAL_MEM_SIZE(reg);
2578 }
2579
2580 if (!priv_dev->onchip_buffers)
2581 priv_dev->onchip_buffers = 256;
2582
2583 max_speed = usb_get_maximum_speed(dev_ofnode(cdns->dev));
2584
2585
2586 switch (max_speed) {
2587 case USB_SPEED_FULL:
2588
2589 case USB_SPEED_HIGH:
2590
2591 case USB_SPEED_SUPER:
2592 break;
2593 default:
2594 dev_err(cdns->dev, "invalid maximum_speed parameter %d\n",
2595 max_speed);
2596
2597 case USB_SPEED_UNKNOWN:
2598
2599 max_speed = USB_SPEED_SUPER;
2600 break;
2601 }
2602
2603
2604 priv_dev->gadget.max_speed = max_speed;
2605 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
2606 priv_dev->gadget.ops = &cdns3_gadget_ops;
2607 priv_dev->gadget.name = "cdns3-gadget";
2608#ifndef __UBOOT__
2609 priv_dev->gadget.name = "usb-ss-gadget";
2610 priv_dev->gadget.sg_supported = 1;
2611 priv_dev->gadget.quirk_avoids_skb_reserve = 1;
2612#endif
2613
2614 spin_lock_init(&priv_dev->lock);
2615 INIT_WORK(&priv_dev->pending_status_wq,
2616 cdns3_pending_setup_status_handler);
2617
2618
2619 INIT_LIST_HEAD(&priv_dev->gadget.ep_list);
2620 INIT_LIST_HEAD(&priv_dev->aligned_buf_list);
2621
2622 ret = cdns3_init_eps(priv_dev);
2623 if (ret) {
2624 dev_err(priv_dev->dev, "Failed to create endpoints\n");
2625 goto err1;
2626 }
2627
2628
2629 priv_dev->setup_buf =
2630 dma_alloc_coherent(8, (unsigned long *)&priv_dev->setup_dma);
2631 if (!priv_dev->setup_buf) {
2632 ret = -ENOMEM;
2633 goto err2;
2634 }
2635
2636 priv_dev->dev_ver = readl(&priv_dev->regs->usb_cap6);
2637
2638 dev_dbg(priv_dev->dev, "Device Controller version: %08x\n",
2639 readl(&priv_dev->regs->usb_cap6));
2640 dev_dbg(priv_dev->dev, "USB Capabilities:: %08x\n",
2641 readl(&priv_dev->regs->usb_cap1));
2642 dev_dbg(priv_dev->dev, "On-Chip memory cnfiguration: %08x\n",
2643 readl(&priv_dev->regs->usb_cap2));
2644
2645 priv_dev->dev_ver = GET_DEV_BASE_VERSION(priv_dev->dev_ver);
2646
2647 priv_dev->zlp_buf = kzalloc(CDNS3_EP_ZLP_BUF_SIZE, GFP_KERNEL);
2648 if (!priv_dev->zlp_buf) {
2649 ret = -ENOMEM;
2650 goto err3;
2651 }
2652
2653
2654 ret = usb_add_gadget_udc((struct device *)priv_dev->dev,
2655 &priv_dev->gadget);
2656 if (ret < 0) {
2657 dev_err(priv_dev->dev,
2658 "Failed to register USB device controller\n");
2659 goto err4;
2660 }
2661
2662 return 0;
2663err4:
2664 kfree(priv_dev->zlp_buf);
2665err3:
2666 dma_free_coherent(priv_dev->setup_buf);
2667err2:
2668 cdns3_free_all_eps(priv_dev);
2669err1:
2670 cdns->gadget_dev = NULL;
2671 return ret;
2672}
2673
2674static int __cdns3_gadget_init(struct cdns3 *cdns)
2675{
2676 int ret = 0;
2677
2678 cdns3_drd_switch_gadget(cdns, 1);
2679
2680 ret = cdns3_gadget_start(cdns);
2681 if (ret)
2682 return ret;
2683
2684 return 0;
2685}
2686
2687static int cdns3_gadget_suspend(struct cdns3 *cdns, bool do_wakeup)
2688{
2689 struct cdns3_device *priv_dev = cdns->gadget_dev;
2690
2691 cdns3_disconnect_gadget(priv_dev);
2692
2693 priv_dev->gadget.speed = USB_SPEED_UNKNOWN;
2694 usb_gadget_set_state(&priv_dev->gadget, USB_STATE_NOTATTACHED);
2695 cdns3_hw_reset_eps_config(priv_dev);
2696
2697
2698 writel(0, &priv_dev->regs->usb_ien);
2699
2700 cdns3_gadget_pullup(&priv_dev->gadget, 0);
2701
2702 return 0;
2703}
2704
2705static int cdns3_gadget_resume(struct cdns3 *cdns, bool hibernated)
2706{
2707 struct cdns3_device *priv_dev = cdns->gadget_dev;
2708
2709 if (!priv_dev->gadget_driver)
2710 return 0;
2711
2712 cdns3_gadget_config(priv_dev);
2713
2714 return 0;
2715}
2716
2717
2718
2719
2720
2721
2722
2723
2724int cdns3_gadget_init(struct cdns3 *cdns)
2725{
2726 struct cdns3_role_driver *rdrv;
2727
2728 rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
2729 if (!rdrv)
2730 return -ENOMEM;
2731
2732 rdrv->start = __cdns3_gadget_init;
2733 rdrv->stop = cdns3_gadget_exit;
2734 rdrv->suspend = cdns3_gadget_suspend;
2735 rdrv->resume = cdns3_gadget_resume;
2736 rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
2737 rdrv->name = "gadget";
2738 cdns->roles[USB_ROLE_DEVICE] = rdrv;
2739
2740 return 0;
2741}
2742
2743
2744
2745
2746
2747
2748
2749static void cdns3_gadget_uboot_handle_interrupt(struct cdns3 *cdns)
2750{
2751 int ret = cdns3_device_irq_handler(0, cdns);
2752
2753 if (ret == IRQ_WAKE_THREAD)
2754 cdns3_device_thread_irq_handler(0, cdns);
2755}
2756
2757int dm_usb_gadget_handle_interrupts(struct udevice *dev)
2758{
2759 struct cdns3 *cdns = dev_get_priv(dev);
2760
2761 cdns3_gadget_uboot_handle_interrupt(cdns);
2762
2763 return 0;
2764}
2765