1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
23#include <linux/platform_device.h>
24#include <linux/dma-mapping.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27#include <linux/delay.h>
28#include <linux/io.h>
29#include <linux/slab.h>
30#include <linux/clk.h>
31#include <linux/regulator/consumer.h>
32
33#include <linux/usb/ch9.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/phy.h>
36#include <linux/platform_data/s3c-hsotg.h>
37
38#include <mach/map.h>
39
40#include "s3c-hsotg.h"
41
42#define DMA_ADDR_INVALID (~((dma_addr_t)0))
43
44static const char * const s3c_hsotg_supply_names[] = {
45 "vusb_d",
46 "vusb_a",
47};
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67#define EP0_MPS_LIMIT 64
68
69struct s3c_hsotg;
70struct s3c_hsotg_req;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109struct s3c_hsotg_ep {
110 struct usb_ep ep;
111 struct list_head queue;
112 struct s3c_hsotg *parent;
113 struct s3c_hsotg_req *req;
114 struct dentry *debugfs;
115
116
117 unsigned long total_data;
118 unsigned int size_loaded;
119 unsigned int last_load;
120 unsigned int fifo_load;
121 unsigned short fifo_size;
122
123 unsigned char dir_in;
124 unsigned char index;
125
126 unsigned int halted:1;
127 unsigned int periodic:1;
128 unsigned int sent_zlp:1;
129
130 char name[10];
131};
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156struct s3c_hsotg {
157 struct device *dev;
158 struct usb_gadget_driver *driver;
159 struct usb_phy *phy;
160 struct s3c_hsotg_plat *plat;
161
162 spinlock_t lock;
163
164 void __iomem *regs;
165 int irq;
166 struct clk *clk;
167
168 struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
169
170 unsigned int dedicated_fifos:1;
171 unsigned char num_of_eps;
172
173 struct dentry *debug_root;
174 struct dentry *debug_file;
175 struct dentry *debug_fifo;
176
177 struct usb_request *ep0_reply;
178 struct usb_request *ctrl_req;
179 u8 ep0_buff[8];
180 u8 ctrl_buff[8];
181
182 struct usb_gadget gadget;
183 unsigned int setup;
184 unsigned long last_rst;
185 struct s3c_hsotg_ep *eps;
186};
187
188
189
190
191
192
193
194
195struct s3c_hsotg_req {
196 struct usb_request req;
197 struct list_head queue;
198 unsigned char in_progress;
199 unsigned char mapped;
200};
201
202
203static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
204{
205 return container_of(req, struct s3c_hsotg_req, req);
206}
207
208static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
209{
210 return container_of(ep, struct s3c_hsotg_ep, ep);
211}
212
213static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
214{
215 return container_of(gadget, struct s3c_hsotg, gadget);
216}
217
218static inline void __orr32(void __iomem *ptr, u32 val)
219{
220 writel(readl(ptr) | val, ptr);
221}
222
223static inline void __bic32(void __iomem *ptr, u32 val)
224{
225 writel(readl(ptr) & ~val, ptr);
226}
227
228
229static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250static inline bool using_dma(struct s3c_hsotg *hsotg)
251{
252 return false;
253}
254
255
256
257
258
259
260static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
261{
262 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
263 u32 new_gsintmsk;
264
265 new_gsintmsk = gsintmsk | ints;
266
267 if (new_gsintmsk != gsintmsk) {
268 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
269 writel(new_gsintmsk, hsotg->regs + GINTMSK);
270 }
271}
272
273
274
275
276
277
278static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
279{
280 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
281 u32 new_gsintmsk;
282
283 new_gsintmsk = gsintmsk & ~ints;
284
285 if (new_gsintmsk != gsintmsk)
286 writel(new_gsintmsk, hsotg->regs + GINTMSK);
287}
288
289
290
291
292
293
294
295
296
297
298
299static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
300 unsigned int ep, unsigned int dir_in,
301 unsigned int en)
302{
303 unsigned long flags;
304 u32 bit = 1 << ep;
305 u32 daint;
306
307 if (!dir_in)
308 bit <<= 16;
309
310 local_irq_save(flags);
311 daint = readl(hsotg->regs + DAINTMSK);
312 if (en)
313 daint |= bit;
314 else
315 daint &= ~bit;
316 writel(daint, hsotg->regs + DAINTMSK);
317 local_irq_restore(flags);
318}
319
320
321
322
323
324static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
325{
326 unsigned int ep;
327 unsigned int addr;
328 unsigned int size;
329 int timeout;
330 u32 val;
331
332
333
334 writel(2048, hsotg->regs + GRXFSIZ);
335 writel(GNPTXFSIZ_NPTxFStAddr(2048) |
336 GNPTXFSIZ_NPTxFDep(1024),
337 hsotg->regs + GNPTXFSIZ);
338
339
340
341
342
343
344
345
346
347 addr = 2048 + 1024;
348 size = 768;
349
350
351
352
353
354
355 for (ep = 1; ep <= 15; ep++) {
356 val = addr;
357 val |= size << DPTXFSIZn_DPTxFSize_SHIFT;
358 addr += size;
359
360 writel(val, hsotg->regs + DPTXFSIZn(ep));
361 }
362
363
364
365
366
367
368 writel(GRSTCTL_TxFNum(0x10) | GRSTCTL_TxFFlsh |
369 GRSTCTL_RxFFlsh, hsotg->regs + GRSTCTL);
370
371
372 timeout = 100;
373 while (1) {
374 val = readl(hsotg->regs + GRSTCTL);
375
376 if ((val & (GRSTCTL_TxFFlsh | GRSTCTL_RxFFlsh)) == 0)
377 break;
378
379 if (--timeout == 0) {
380 dev_err(hsotg->dev,
381 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
382 __func__, val);
383 }
384
385 udelay(1);
386 }
387
388 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
389}
390
391
392
393
394
395
396
397static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
398 gfp_t flags)
399{
400 struct s3c_hsotg_req *req;
401
402 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
403 if (!req)
404 return NULL;
405
406 INIT_LIST_HEAD(&req->queue);
407
408 req->req.dma = DMA_ADDR_INVALID;
409 return &req->req;
410}
411
412
413
414
415
416
417
418
419static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
420{
421 return hs_ep->periodic;
422}
423
424
425
426
427
428
429
430
431
432
433static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
434 struct s3c_hsotg_ep *hs_ep,
435 struct s3c_hsotg_req *hs_req)
436{
437 struct usb_request *req = &hs_req->req;
438 enum dma_data_direction dir;
439
440 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
441
442
443 if (hs_req->req.length == 0)
444 return;
445
446 if (hs_req->mapped) {
447
448
449 dma_unmap_single(hsotg->dev, req->dma, req->length, dir);
450
451 req->dma = DMA_ADDR_INVALID;
452 hs_req->mapped = 0;
453 } else {
454 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
455 }
456}
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
475 struct s3c_hsotg_ep *hs_ep,
476 struct s3c_hsotg_req *hs_req)
477{
478 bool periodic = is_ep_periodic(hs_ep);
479 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
480 int buf_pos = hs_req->req.actual;
481 int to_write = hs_ep->size_loaded;
482 void *data;
483 int can_write;
484 int pkt_round;
485
486 to_write -= (buf_pos - hs_ep->last_load);
487
488
489 if (to_write == 0)
490 return 0;
491
492 if (periodic && !hsotg->dedicated_fifos) {
493 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
494 int size_left;
495 int size_done;
496
497
498
499
500
501
502 size_left = DxEPTSIZ_XferSize_GET(epsize);
503
504
505
506
507
508 if (hs_ep->fifo_load != 0) {
509 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
510 return -ENOSPC;
511 }
512
513 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
514 __func__, size_left,
515 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
516
517
518 size_done = hs_ep->size_loaded - size_left;
519
520
521 can_write = hs_ep->fifo_load - size_done;
522 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
523 __func__, can_write);
524
525 can_write = hs_ep->fifo_size - can_write;
526 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
527 __func__, can_write);
528
529 if (can_write <= 0) {
530 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
531 return -ENOSPC;
532 }
533 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
534 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
535
536 can_write &= 0xffff;
537 can_write *= 4;
538 } else {
539 if (GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
540 dev_dbg(hsotg->dev,
541 "%s: no queue slots available (0x%08x)\n",
542 __func__, gnptxsts);
543
544 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTxFEmp);
545 return -ENOSPC;
546 }
547
548 can_write = GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
549 can_write *= 4;
550 }
551
552 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n",
553 __func__, gnptxsts, can_write, to_write, hs_ep->ep.maxpacket);
554
555
556
557
558
559
560 if (can_write > 512)
561 can_write = 512;
562
563
564
565
566
567
568 if (to_write > hs_ep->ep.maxpacket) {
569 to_write = hs_ep->ep.maxpacket;
570
571 s3c_hsotg_en_gsint(hsotg,
572 periodic ? GINTSTS_PTxFEmp :
573 GINTSTS_NPTxFEmp);
574 }
575
576
577
578 if (to_write > can_write) {
579 to_write = can_write;
580 pkt_round = to_write % hs_ep->ep.maxpacket;
581
582
583
584
585
586
587
588
589
590 if (pkt_round)
591 to_write -= pkt_round;
592
593
594
595
596
597
598 s3c_hsotg_en_gsint(hsotg,
599 periodic ? GINTSTS_PTxFEmp :
600 GINTSTS_NPTxFEmp);
601 }
602
603 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
604 to_write, hs_req->req.length, can_write, buf_pos);
605
606 if (to_write <= 0)
607 return -ENOSPC;
608
609 hs_req->req.actual = buf_pos + to_write;
610 hs_ep->total_data += to_write;
611
612 if (periodic)
613 hs_ep->fifo_load += to_write;
614
615 to_write = DIV_ROUND_UP(to_write, 4);
616 data = hs_req->req.buf + buf_pos;
617
618 writesl(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
619
620 return (to_write >= can_write) ? -ENOSPC : 0;
621}
622
623
624
625
626
627
628
629
630static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
631{
632 int index = hs_ep->index;
633 unsigned maxsize;
634 unsigned maxpkt;
635
636 if (index != 0) {
637 maxsize = DxEPTSIZ_XferSize_LIMIT + 1;
638 maxpkt = DxEPTSIZ_PktCnt_LIMIT + 1;
639 } else {
640 maxsize = 64+64;
641 if (hs_ep->dir_in)
642 maxpkt = DIEPTSIZ0_PktCnt_LIMIT + 1;
643 else
644 maxpkt = 2;
645 }
646
647
648 maxpkt--;
649 maxsize--;
650
651
652
653
654
655
656 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
657 maxsize = maxpkt * hs_ep->ep.maxpacket;
658
659 return maxsize;
660}
661
662
663
664
665
666
667
668
669
670
671
672static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
673 struct s3c_hsotg_ep *hs_ep,
674 struct s3c_hsotg_req *hs_req,
675 bool continuing)
676{
677 struct usb_request *ureq = &hs_req->req;
678 int index = hs_ep->index;
679 int dir_in = hs_ep->dir_in;
680 u32 epctrl_reg;
681 u32 epsize_reg;
682 u32 epsize;
683 u32 ctrl;
684 unsigned length;
685 unsigned packets;
686 unsigned maxreq;
687
688 if (index != 0) {
689 if (hs_ep->req && !continuing) {
690 dev_err(hsotg->dev, "%s: active request\n", __func__);
691 WARN_ON(1);
692 return;
693 } else if (hs_ep->req != hs_req && continuing) {
694 dev_err(hsotg->dev,
695 "%s: continue different req\n", __func__);
696 WARN_ON(1);
697 return;
698 }
699 }
700
701 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
702 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
703
704 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
705 __func__, readl(hsotg->regs + epctrl_reg), index,
706 hs_ep->dir_in ? "in" : "out");
707
708
709 ctrl = readl(hsotg->regs + epctrl_reg);
710
711 if (ctrl & DxEPCTL_Stall) {
712 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
713 return;
714 }
715
716 length = ureq->length - ureq->actual;
717 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
718 ureq->length, ureq->actual);
719 if (0)
720 dev_dbg(hsotg->dev,
721 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
722 ureq->buf, length, ureq->dma,
723 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
724
725 maxreq = get_ep_limit(hs_ep);
726 if (length > maxreq) {
727 int round = maxreq % hs_ep->ep.maxpacket;
728
729 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
730 __func__, length, maxreq, round);
731
732
733 if (round)
734 maxreq -= round;
735
736 length = maxreq;
737 }
738
739 if (length)
740 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
741 else
742 packets = 1;
743
744 if (dir_in && index != 0)
745 epsize = DxEPTSIZ_MC(1);
746 else
747 epsize = 0;
748
749 if (index != 0 && ureq->zero) {
750
751
752
753
754
755 if (length == (packets * hs_ep->ep.maxpacket))
756 packets++;
757 }
758
759 epsize |= DxEPTSIZ_PktCnt(packets);
760 epsize |= DxEPTSIZ_XferSize(length);
761
762 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
763 __func__, packets, length, ureq->length, epsize, epsize_reg);
764
765
766 hs_ep->req = hs_req;
767
768
769 writel(epsize, hsotg->regs + epsize_reg);
770
771 if (using_dma(hsotg) && !continuing) {
772 unsigned int dma_reg;
773
774
775
776
777
778
779 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
780 writel(ureq->dma, hsotg->regs + dma_reg);
781
782 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
783 __func__, ureq->dma, dma_reg);
784 }
785
786 ctrl |= DxEPCTL_EPEna;
787 ctrl |= DxEPCTL_USBActEp;
788
789 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
790
791
792 if (hsotg->setup && index == 0)
793 hsotg->setup = 0;
794 else
795 ctrl |= DxEPCTL_CNAK;
796
797
798 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
799 writel(ctrl, hsotg->regs + epctrl_reg);
800
801
802
803
804
805
806 hs_ep->size_loaded = length;
807 hs_ep->last_load = ureq->actual;
808
809 if (dir_in && !using_dma(hsotg)) {
810
811 hs_ep->fifo_load = 0;
812
813 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
814 }
815
816
817
818
819
820 if (dir_in)
821 writel(DIEPMSK_INTknTXFEmpMsk,
822 hsotg->regs + DIEPINT(index));
823
824
825
826
827
828
829
830 if (!(readl(hsotg->regs + epctrl_reg) & DxEPCTL_EPEna))
831 dev_warn(hsotg->dev,
832 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
833 index, readl(hsotg->regs + epctrl_reg));
834
835 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
836 __func__, readl(hsotg->regs + epctrl_reg));
837}
838
839
840
841
842
843
844
845
846
847
848
849
850
851static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
852 struct s3c_hsotg_ep *hs_ep,
853 struct usb_request *req)
854{
855 enum dma_data_direction dir;
856 struct s3c_hsotg_req *hs_req = our_req(req);
857
858 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
859
860
861 if (hs_req->req.length == 0)
862 return 0;
863
864 if (req->dma == DMA_ADDR_INVALID) {
865 dma_addr_t dma;
866
867 dma = dma_map_single(hsotg->dev, req->buf, req->length, dir);
868
869 if (unlikely(dma_mapping_error(hsotg->dev, dma)))
870 goto dma_error;
871
872 if (dma & 3) {
873 dev_err(hsotg->dev, "%s: unaligned dma buffer\n",
874 __func__);
875
876 dma_unmap_single(hsotg->dev, dma, req->length, dir);
877 return -EINVAL;
878 }
879
880 hs_req->mapped = 1;
881 req->dma = dma;
882 } else {
883 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
884 hs_req->mapped = 0;
885 }
886
887 return 0;
888
889dma_error:
890 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
891 __func__, req->buf, req->length);
892
893 return -EIO;
894}
895
896static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
897 gfp_t gfp_flags)
898{
899 struct s3c_hsotg_req *hs_req = our_req(req);
900 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
901 struct s3c_hsotg *hs = hs_ep->parent;
902 bool first;
903
904 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
905 ep->name, req, req->length, req->buf, req->no_interrupt,
906 req->zero, req->short_not_ok);
907
908
909 INIT_LIST_HEAD(&hs_req->queue);
910 req->actual = 0;
911 req->status = -EINPROGRESS;
912
913
914 if (using_dma(hs)) {
915 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
916 if (ret)
917 return ret;
918 }
919
920 first = list_empty(&hs_ep->queue);
921 list_add_tail(&hs_req->queue, &hs_ep->queue);
922
923 if (first)
924 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
925
926 return 0;
927}
928
929static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
930 gfp_t gfp_flags)
931{
932 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
933 struct s3c_hsotg *hs = hs_ep->parent;
934 unsigned long flags = 0;
935 int ret = 0;
936
937 spin_lock_irqsave(&hs->lock, flags);
938 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
939 spin_unlock_irqrestore(&hs->lock, flags);
940
941 return ret;
942}
943
944static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
945 struct usb_request *req)
946{
947 struct s3c_hsotg_req *hs_req = our_req(req);
948
949 kfree(hs_req);
950}
951
952
953
954
955
956
957
958
959
960static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
961 struct usb_request *req)
962{
963 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
964 struct s3c_hsotg *hsotg = hs_ep->parent;
965
966 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
967
968 s3c_hsotg_ep_free_request(ep, req);
969}
970
971
972
973
974
975
976
977
978
979static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
980 u32 windex)
981{
982 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
983 int dir = (windex & USB_DIR_IN) ? 1 : 0;
984 int idx = windex & 0x7F;
985
986 if (windex >= 0x100)
987 return NULL;
988
989 if (idx > hsotg->num_of_eps)
990 return NULL;
991
992 if (idx && ep->dir_in != dir)
993 return NULL;
994
995 return ep;
996}
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
1009 struct s3c_hsotg_ep *ep,
1010 void *buff,
1011 int length)
1012{
1013 struct usb_request *req;
1014 int ret;
1015
1016 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1017
1018 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1019 hsotg->ep0_reply = req;
1020 if (!req) {
1021 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1022 return -ENOMEM;
1023 }
1024
1025 req->buf = hsotg->ep0_buff;
1026 req->length = length;
1027 req->zero = 1;
1028 req->complete = s3c_hsotg_complete_oursetup;
1029
1030 if (length)
1031 memcpy(req->buf, buff, length);
1032 else
1033 ep->sent_zlp = 1;
1034
1035 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1036 if (ret) {
1037 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1038 return ret;
1039 }
1040
1041 return 0;
1042}
1043
1044
1045
1046
1047
1048
1049static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1050 struct usb_ctrlrequest *ctrl)
1051{
1052 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1053 struct s3c_hsotg_ep *ep;
1054 __le16 reply;
1055 int ret;
1056
1057 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1058
1059 if (!ep0->dir_in) {
1060 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1061 return -EINVAL;
1062 }
1063
1064 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1065 case USB_RECIP_DEVICE:
1066 reply = cpu_to_le16(0);
1067
1068 break;
1069
1070 case USB_RECIP_INTERFACE:
1071
1072 reply = cpu_to_le16(0);
1073 break;
1074
1075 case USB_RECIP_ENDPOINT:
1076 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1077 if (!ep)
1078 return -ENOENT;
1079
1080 reply = cpu_to_le16(ep->halted ? 1 : 0);
1081 break;
1082
1083 default:
1084 return 0;
1085 }
1086
1087 if (le16_to_cpu(ctrl->wLength) != 2)
1088 return -EINVAL;
1089
1090 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1091 if (ret) {
1092 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1093 return ret;
1094 }
1095
1096 return 1;
1097}
1098
1099static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1100
1101
1102
1103
1104
1105
1106
1107static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1108{
1109 if (list_empty(&hs_ep->queue))
1110 return NULL;
1111
1112 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1113}
1114
1115
1116
1117
1118
1119
1120static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1121 struct usb_ctrlrequest *ctrl)
1122{
1123 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1124 struct s3c_hsotg_req *hs_req;
1125 bool restart;
1126 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1127 struct s3c_hsotg_ep *ep;
1128 int ret;
1129
1130 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1131 __func__, set ? "SET" : "CLEAR");
1132
1133 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1134 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1135 if (!ep) {
1136 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1137 __func__, le16_to_cpu(ctrl->wIndex));
1138 return -ENOENT;
1139 }
1140
1141 switch (le16_to_cpu(ctrl->wValue)) {
1142 case USB_ENDPOINT_HALT:
1143 s3c_hsotg_ep_sethalt(&ep->ep, set);
1144
1145 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1146 if (ret) {
1147 dev_err(hsotg->dev,
1148 "%s: failed to send reply\n", __func__);
1149 return ret;
1150 }
1151
1152 if (!set) {
1153
1154
1155
1156
1157 if (ep->req) {
1158 hs_req = ep->req;
1159 ep->req = NULL;
1160 list_del_init(&hs_req->queue);
1161 hs_req->req.complete(&ep->ep,
1162 &hs_req->req);
1163 }
1164
1165
1166 restart = !list_empty(&ep->queue);
1167 if (restart) {
1168 hs_req = get_ep_head(ep);
1169 s3c_hsotg_start_req(hsotg, ep,
1170 hs_req, false);
1171 }
1172 }
1173
1174 break;
1175
1176 default:
1177 return -ENOENT;
1178 }
1179 } else
1180 return -ENOENT;
1181
1182 return 1;
1183}
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1195 struct usb_ctrlrequest *ctrl)
1196{
1197 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1198 int ret = 0;
1199 u32 dcfg;
1200
1201 ep0->sent_zlp = 0;
1202
1203 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1204 ctrl->bRequest, ctrl->bRequestType,
1205 ctrl->wValue, ctrl->wLength);
1206
1207
1208
1209
1210
1211
1212 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1213 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1214
1215
1216
1217
1218
1219 if (ctrl->wLength == 0)
1220 ep0->dir_in = 1;
1221
1222 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1223 switch (ctrl->bRequest) {
1224 case USB_REQ_SET_ADDRESS:
1225 dcfg = readl(hsotg->regs + DCFG);
1226 dcfg &= ~DCFG_DevAddr_MASK;
1227 dcfg |= ctrl->wValue << DCFG_DevAddr_SHIFT;
1228 writel(dcfg, hsotg->regs + DCFG);
1229
1230 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1231
1232 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1233 return;
1234
1235 case USB_REQ_GET_STATUS:
1236 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1237 break;
1238
1239 case USB_REQ_CLEAR_FEATURE:
1240 case USB_REQ_SET_FEATURE:
1241 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1242 break;
1243 }
1244 }
1245
1246
1247
1248 if (ret == 0 && hsotg->driver) {
1249 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1250 if (ret < 0)
1251 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1252 }
1253
1254
1255
1256
1257
1258
1259 if (ret < 0) {
1260 u32 reg;
1261 u32 ctrl;
1262
1263 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1264 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1265
1266
1267
1268
1269
1270
1271 ctrl = readl(hsotg->regs + reg);
1272 ctrl |= DxEPCTL_Stall;
1273 ctrl |= DxEPCTL_CNAK;
1274 writel(ctrl, hsotg->regs + reg);
1275
1276 dev_dbg(hsotg->dev,
1277 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
1278 ctrl, reg, readl(hsotg->regs + reg));
1279
1280
1281
1282
1283
1284 }
1285}
1286
1287static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1298 struct usb_request *req)
1299{
1300 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1301 struct s3c_hsotg *hsotg = hs_ep->parent;
1302
1303 if (req->status < 0) {
1304 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1305 return;
1306 }
1307
1308 if (req->actual == 0)
1309 s3c_hsotg_enqueue_setup(hsotg);
1310 else
1311 s3c_hsotg_process_control(hsotg, req->buf);
1312}
1313
1314
1315
1316
1317
1318
1319
1320
1321static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1322{
1323 struct usb_request *req = hsotg->ctrl_req;
1324 struct s3c_hsotg_req *hs_req = our_req(req);
1325 int ret;
1326
1327 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1328
1329 req->zero = 0;
1330 req->length = 8;
1331 req->buf = hsotg->ctrl_buff;
1332 req->complete = s3c_hsotg_complete_setup;
1333
1334 if (!list_empty(&hs_req->queue)) {
1335 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1336 return;
1337 }
1338
1339 hsotg->eps[0].dir_in = 0;
1340
1341 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1342 if (ret < 0) {
1343 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
1344
1345
1346
1347
1348 }
1349}
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1365 struct s3c_hsotg_ep *hs_ep,
1366 struct s3c_hsotg_req *hs_req,
1367 int result)
1368{
1369 bool restart;
1370
1371 if (!hs_req) {
1372 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1373 return;
1374 }
1375
1376 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1377 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1378
1379
1380
1381
1382
1383
1384 if (hs_req->req.status == -EINPROGRESS)
1385 hs_req->req.status = result;
1386
1387 hs_ep->req = NULL;
1388 list_del_init(&hs_req->queue);
1389
1390 if (using_dma(hsotg))
1391 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1392
1393
1394
1395
1396
1397
1398 if (hs_req->req.complete) {
1399 spin_unlock(&hsotg->lock);
1400 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
1401 spin_lock(&hsotg->lock);
1402 }
1403
1404
1405
1406
1407
1408
1409
1410 if (!hs_ep->req && result >= 0) {
1411 restart = !list_empty(&hs_ep->queue);
1412 if (restart) {
1413 hs_req = get_ep_head(hs_ep);
1414 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1415 }
1416 }
1417}
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1430{
1431 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1432 struct s3c_hsotg_req *hs_req = hs_ep->req;
1433 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
1434 int to_read;
1435 int max_req;
1436 int read_ptr;
1437
1438
1439 if (!hs_req) {
1440 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
1441 int ptr;
1442
1443 dev_warn(hsotg->dev,
1444 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1445 __func__, size, ep_idx, epctl);
1446
1447
1448 for (ptr = 0; ptr < size; ptr += 4)
1449 (void)readl(fifo);
1450
1451 return;
1452 }
1453
1454 to_read = size;
1455 read_ptr = hs_req->req.actual;
1456 max_req = hs_req->req.length - read_ptr;
1457
1458 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1459 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1460
1461 if (to_read > max_req) {
1462
1463
1464
1465
1466
1467
1468 WARN_ON_ONCE(1);
1469 }
1470
1471 hs_ep->total_data += to_read;
1472 hs_req->req.actual += to_read;
1473 to_read = DIV_ROUND_UP(to_read, 4);
1474
1475
1476
1477
1478
1479 readsl(fifo, hs_req->req.buf + read_ptr, to_read);
1480}
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1495 struct s3c_hsotg_req *req)
1496{
1497 u32 ctrl;
1498
1499 if (!req) {
1500 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1501 return;
1502 }
1503
1504 if (req->req.length == 0) {
1505 hsotg->eps[0].sent_zlp = 1;
1506 s3c_hsotg_enqueue_setup(hsotg);
1507 return;
1508 }
1509
1510 hsotg->eps[0].dir_in = 1;
1511 hsotg->eps[0].sent_zlp = 1;
1512
1513 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1514
1515
1516 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
1517 DxEPTSIZ_XferSize(0), hsotg->regs + DIEPTSIZ(0));
1518
1519 ctrl = readl(hsotg->regs + DIEPCTL0);
1520 ctrl |= DxEPCTL_CNAK;
1521 ctrl |= DxEPCTL_EPEna;
1522 ctrl |= DxEPCTL_USBActEp;
1523 writel(ctrl, hsotg->regs + DIEPCTL0);
1524}
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1537 int epnum, bool was_setup)
1538{
1539 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
1540 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1541 struct s3c_hsotg_req *hs_req = hs_ep->req;
1542 struct usb_request *req = &hs_req->req;
1543 unsigned size_left = DxEPTSIZ_XferSize_GET(epsize);
1544 int result = 0;
1545
1546 if (!hs_req) {
1547 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1548 return;
1549 }
1550
1551 if (using_dma(hsotg)) {
1552 unsigned size_done;
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563 size_done = hs_ep->size_loaded - size_left;
1564 size_done += hs_ep->last_load;
1565
1566 req->actual = size_done;
1567 }
1568
1569
1570 if (req->actual < req->length && size_left == 0) {
1571 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1572 return;
1573 } else if (epnum == 0) {
1574
1575
1576
1577
1578 hsotg->setup = was_setup ? 0 : 1;
1579 }
1580
1581 if (req->actual < req->length && req->short_not_ok) {
1582 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1583 __func__, req->actual, req->length);
1584
1585
1586
1587
1588
1589 }
1590
1591 if (epnum == 0) {
1592
1593
1594
1595
1596 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1597 s3c_hsotg_send_zlp(hsotg, hs_req);
1598 }
1599
1600 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
1601}
1602
1603
1604
1605
1606
1607
1608
1609static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1610{
1611 u32 dsts;
1612
1613 dsts = readl(hsotg->regs + DSTS);
1614 dsts &= DSTS_SOFFN_MASK;
1615 dsts >>= DSTS_SOFFN_SHIFT;
1616
1617 return dsts;
1618}
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
1637{
1638 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
1639 u32 epnum, status, size;
1640
1641 WARN_ON(using_dma(hsotg));
1642
1643 epnum = grxstsr & GRXSTS_EPNum_MASK;
1644 status = grxstsr & GRXSTS_PktSts_MASK;
1645
1646 size = grxstsr & GRXSTS_ByteCnt_MASK;
1647 size >>= GRXSTS_ByteCnt_SHIFT;
1648
1649 if (1)
1650 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1651 __func__, grxstsr, size, epnum);
1652
1653#define __status(x) ((x) >> GRXSTS_PktSts_SHIFT)
1654
1655 switch (status >> GRXSTS_PktSts_SHIFT) {
1656 case __status(GRXSTS_PktSts_GlobalOutNAK):
1657 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1658 break;
1659
1660 case __status(GRXSTS_PktSts_OutDone):
1661 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1662 s3c_hsotg_read_frameno(hsotg));
1663
1664 if (!using_dma(hsotg))
1665 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1666 break;
1667
1668 case __status(GRXSTS_PktSts_SetupDone):
1669 dev_dbg(hsotg->dev,
1670 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1671 s3c_hsotg_read_frameno(hsotg),
1672 readl(hsotg->regs + DOEPCTL(0)));
1673
1674 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1675 break;
1676
1677 case __status(GRXSTS_PktSts_OutRX):
1678 s3c_hsotg_rx_data(hsotg, epnum, size);
1679 break;
1680
1681 case __status(GRXSTS_PktSts_SetupRX):
1682 dev_dbg(hsotg->dev,
1683 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1684 s3c_hsotg_read_frameno(hsotg),
1685 readl(hsotg->regs + DOEPCTL(0)));
1686
1687 s3c_hsotg_rx_data(hsotg, epnum, size);
1688 break;
1689
1690 default:
1691 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1692 __func__, grxstsr);
1693
1694 s3c_hsotg_dump(hsotg);
1695 break;
1696 }
1697}
1698
1699
1700
1701
1702
1703static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1704{
1705 switch (mps) {
1706 case 64:
1707 return D0EPCTL_MPS_64;
1708 case 32:
1709 return D0EPCTL_MPS_32;
1710 case 16:
1711 return D0EPCTL_MPS_16;
1712 case 8:
1713 return D0EPCTL_MPS_8;
1714 }
1715
1716
1717 WARN_ON(1);
1718 return (u32)-1;
1719}
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1731 unsigned int ep, unsigned int mps)
1732{
1733 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1734 void __iomem *regs = hsotg->regs;
1735 u32 mpsval;
1736 u32 reg;
1737
1738 if (ep == 0) {
1739
1740 mpsval = s3c_hsotg_ep0_mps(mps);
1741 if (mpsval > 3)
1742 goto bad_mps;
1743 } else {
1744 if (mps >= DxEPCTL_MPS_LIMIT+1)
1745 goto bad_mps;
1746
1747 mpsval = mps;
1748 }
1749
1750 hs_ep->ep.maxpacket = mps;
1751
1752
1753
1754
1755
1756
1757 reg = readl(regs + DIEPCTL(ep));
1758 reg &= ~DxEPCTL_MPS_MASK;
1759 reg |= mpsval;
1760 writel(reg, regs + DIEPCTL(ep));
1761
1762 if (ep) {
1763 reg = readl(regs + DOEPCTL(ep));
1764 reg &= ~DxEPCTL_MPS_MASK;
1765 reg |= mpsval;
1766 writel(reg, regs + DOEPCTL(ep));
1767 }
1768
1769 return;
1770
1771bad_mps:
1772 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1773}
1774
1775
1776
1777
1778
1779
1780static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1781{
1782 int timeout;
1783 int val;
1784
1785 writel(GRSTCTL_TxFNum(idx) | GRSTCTL_TxFFlsh,
1786 hsotg->regs + GRSTCTL);
1787
1788
1789 timeout = 100;
1790
1791 while (1) {
1792 val = readl(hsotg->regs + GRSTCTL);
1793
1794 if ((val & (GRSTCTL_TxFFlsh)) == 0)
1795 break;
1796
1797 if (--timeout == 0) {
1798 dev_err(hsotg->dev,
1799 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1800 __func__, val);
1801 }
1802
1803 udelay(1);
1804 }
1805}
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1816 struct s3c_hsotg_ep *hs_ep)
1817{
1818 struct s3c_hsotg_req *hs_req = hs_ep->req;
1819
1820 if (!hs_ep->dir_in || !hs_req)
1821 return 0;
1822
1823 if (hs_req->req.actual < hs_req->req.length) {
1824 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1825 hs_ep->index);
1826 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1827 }
1828
1829 return 0;
1830}
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1841 struct s3c_hsotg_ep *hs_ep)
1842{
1843 struct s3c_hsotg_req *hs_req = hs_ep->req;
1844 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
1845 int size_left, size_done;
1846
1847 if (!hs_req) {
1848 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1849 return;
1850 }
1851
1852
1853 if (hsotg->eps[0].sent_zlp) {
1854 dev_dbg(hsotg->dev, "zlp packet received\n");
1855 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1856 return;
1857 }
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869 size_left = DxEPTSIZ_XferSize_GET(epsize);
1870
1871 size_done = hs_ep->size_loaded - size_left;
1872 size_done += hs_ep->last_load;
1873
1874 if (hs_req->req.actual != size_done)
1875 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1876 __func__, hs_req->req.actual, size_done);
1877
1878 hs_req->req.actual = size_done;
1879 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1880 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1893 hs_req->req.length == hs_req->req.actual &&
1894 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1895
1896 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1897 s3c_hsotg_send_zlp(hsotg, hs_req);
1898
1899 return;
1900 }
1901
1902 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1903 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1904 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1905 } else
1906 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1907}
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1918 int dir_in)
1919{
1920 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
1921 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1922 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1923 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
1924 u32 ints;
1925
1926 ints = readl(hsotg->regs + epint_reg);
1927
1928
1929 writel(ints, hsotg->regs + epint_reg);
1930
1931 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1932 __func__, idx, dir_in ? "in" : "out", ints);
1933
1934 if (ints & DxEPINT_XferCompl) {
1935 dev_dbg(hsotg->dev,
1936 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1937 __func__, readl(hsotg->regs + epctl_reg),
1938 readl(hsotg->regs + epsiz_reg));
1939
1940
1941
1942
1943
1944 if (dir_in) {
1945 s3c_hsotg_complete_in(hsotg, hs_ep);
1946
1947 if (idx == 0 && !hs_ep->req)
1948 s3c_hsotg_enqueue_setup(hsotg);
1949 } else if (using_dma(hsotg)) {
1950
1951
1952
1953
1954
1955 s3c_hsotg_handle_outdone(hsotg, idx, false);
1956 }
1957 }
1958
1959 if (ints & DxEPINT_EPDisbld) {
1960 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
1961
1962 if (dir_in) {
1963 int epctl = readl(hsotg->regs + epctl_reg);
1964
1965 s3c_hsotg_txfifo_flush(hsotg, idx);
1966
1967 if ((epctl & DxEPCTL_Stall) &&
1968 (epctl & DxEPCTL_EPType_Bulk)) {
1969 int dctl = readl(hsotg->regs + DCTL);
1970
1971 dctl |= DCTL_CGNPInNAK;
1972 writel(dctl, hsotg->regs + DCTL);
1973 }
1974 }
1975 }
1976
1977 if (ints & DxEPINT_AHBErr)
1978 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
1979
1980 if (ints & DxEPINT_Setup) {
1981 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1982
1983 if (using_dma(hsotg) && idx == 0) {
1984
1985
1986
1987
1988
1989
1990
1991 if (dir_in)
1992 WARN_ON_ONCE(1);
1993 else
1994 s3c_hsotg_handle_outdone(hsotg, 0, true);
1995 }
1996 }
1997
1998 if (ints & DxEPINT_Back2BackSetup)
1999 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
2000
2001 if (dir_in) {
2002
2003 if (ints & DIEPMSK_INTknTXFEmpMsk) {
2004 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2005 __func__, idx);
2006 }
2007
2008
2009 if (ints & DIEPMSK_INTknEPMisMsk) {
2010 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2011 __func__, idx);
2012 }
2013
2014
2015 if (hsotg->dedicated_fifos &&
2016 ints & DIEPMSK_TxFIFOEmpty) {
2017 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2018 __func__, idx);
2019 if (!using_dma(hsotg))
2020 s3c_hsotg_trytx(hsotg, hs_ep);
2021 }
2022 }
2023}
2024
2025
2026
2027
2028
2029
2030
2031
2032static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
2033{
2034 u32 dsts = readl(hsotg->regs + DSTS);
2035 int ep0_mps = 0, ep_mps;
2036
2037
2038
2039
2040
2041
2042
2043 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2044
2045
2046
2047
2048
2049
2050
2051
2052 switch (dsts & DSTS_EnumSpd_MASK) {
2053 case DSTS_EnumSpd_FS:
2054 case DSTS_EnumSpd_FS48:
2055 hsotg->gadget.speed = USB_SPEED_FULL;
2056 ep0_mps = EP0_MPS_LIMIT;
2057 ep_mps = 64;
2058 break;
2059
2060 case DSTS_EnumSpd_HS:
2061 hsotg->gadget.speed = USB_SPEED_HIGH;
2062 ep0_mps = EP0_MPS_LIMIT;
2063 ep_mps = 512;
2064 break;
2065
2066 case DSTS_EnumSpd_LS:
2067 hsotg->gadget.speed = USB_SPEED_LOW;
2068
2069
2070
2071
2072
2073 break;
2074 }
2075 dev_info(hsotg->dev, "new device is %s\n",
2076 usb_speed_string(hsotg->gadget.speed));
2077
2078
2079
2080
2081
2082
2083 if (ep0_mps) {
2084 int i;
2085 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
2086 for (i = 1; i < hsotg->num_of_eps; i++)
2087 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
2088 }
2089
2090
2091
2092 s3c_hsotg_enqueue_setup(hsotg);
2093
2094 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2095 readl(hsotg->regs + DIEPCTL0),
2096 readl(hsotg->regs + DOEPCTL0));
2097}
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109static void kill_all_requests(struct s3c_hsotg *hsotg,
2110 struct s3c_hsotg_ep *ep,
2111 int result, bool force)
2112{
2113 struct s3c_hsotg_req *req, *treq;
2114
2115 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2116
2117
2118
2119
2120
2121 if (ep->req == req && ep->dir_in && !force)
2122 continue;
2123
2124 s3c_hsotg_complete_request(hsotg, ep, req,
2125 result);
2126 }
2127}
2128
2129#define call_gadget(_hs, _entry) \
2130 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
2131 (_hs)->driver && (_hs)->driver->_entry) { \
2132 spin_unlock(&_hs->lock); \
2133 (_hs)->driver->_entry(&(_hs)->gadget); \
2134 spin_lock(&_hs->lock); \
2135 }
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
2146{
2147 unsigned ep;
2148
2149 for (ep = 0; ep < hsotg->num_of_eps; ep++)
2150 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2151
2152 call_gadget(hsotg, disconnect);
2153}
2154
2155
2156
2157
2158
2159
2160static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2161{
2162 struct s3c_hsotg_ep *ep;
2163 int epno, ret;
2164
2165
2166
2167 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
2168 ep = &hsotg->eps[epno];
2169
2170 if (!ep->dir_in)
2171 continue;
2172
2173 if ((periodic && !ep->periodic) ||
2174 (!periodic && ep->periodic))
2175 continue;
2176
2177 ret = s3c_hsotg_trytx(hsotg, ep);
2178 if (ret < 0)
2179 break;
2180 }
2181}
2182
2183
2184#define IRQ_RETRY_MASK (GINTSTS_NPTxFEmp | \
2185 GINTSTS_PTxFEmp | \
2186 GINTSTS_RxFLvl)
2187
2188
2189
2190
2191
2192
2193
2194static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2195{
2196 int timeout;
2197 u32 grstctl;
2198
2199 dev_dbg(hsotg->dev, "resetting core\n");
2200
2201
2202 writel(GRSTCTL_CSftRst, hsotg->regs + GRSTCTL);
2203
2204 timeout = 10000;
2205 do {
2206 grstctl = readl(hsotg->regs + GRSTCTL);
2207 } while ((grstctl & GRSTCTL_CSftRst) && timeout-- > 0);
2208
2209 if (grstctl & GRSTCTL_CSftRst) {
2210 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2211 return -EINVAL;
2212 }
2213
2214 timeout = 10000;
2215
2216 while (1) {
2217 u32 grstctl = readl(hsotg->regs + GRSTCTL);
2218
2219 if (timeout-- < 0) {
2220 dev_info(hsotg->dev,
2221 "%s: reset failed, GRSTCTL=%08x\n",
2222 __func__, grstctl);
2223 return -ETIMEDOUT;
2224 }
2225
2226 if (!(grstctl & GRSTCTL_AHBIdle))
2227 continue;
2228
2229 break;
2230 }
2231
2232 dev_dbg(hsotg->dev, "reset successful\n");
2233 return 0;
2234}
2235
2236
2237
2238
2239
2240
2241
2242static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2243{
2244 s3c_hsotg_corereset(hsotg);
2245
2246
2247
2248
2249
2250
2251
2252 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) |
2253 (0x5 << 10), hsotg->regs + GUSBCFG);
2254
2255 s3c_hsotg_init_fifo(hsotg);
2256
2257 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
2258
2259 writel(1 << 18 | DCFG_DevSpd_HS, hsotg->regs + DCFG);
2260
2261
2262 writel(0xffffffff, hsotg->regs + GOTGINT);
2263
2264
2265 writel(0xffffffff, hsotg->regs + GINTSTS);
2266
2267 writel(GINTSTS_ErlySusp | GINTSTS_SessReqInt |
2268 GINTSTS_GOUTNakEff | GINTSTS_GINNakEff |
2269 GINTSTS_ConIDStsChng | GINTSTS_USBRst |
2270 GINTSTS_EnumDone | GINTSTS_OTGInt |
2271 GINTSTS_USBSusp | GINTSTS_WkUpInt,
2272 hsotg->regs + GINTMSK);
2273
2274 if (using_dma(hsotg))
2275 writel(GAHBCFG_GlblIntrEn | GAHBCFG_DMAEn |
2276 GAHBCFG_HBstLen_Incr4,
2277 hsotg->regs + GAHBCFG);
2278 else
2279 writel(GAHBCFG_GlblIntrEn, hsotg->regs + GAHBCFG);
2280
2281
2282
2283
2284
2285
2286
2287 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TxFIFOEmpty : 0) |
2288 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk |
2289 DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2290 DIEPMSK_INTknEPMisMsk,
2291 hsotg->regs + DIEPMSK);
2292
2293
2294
2295
2296
2297 writel((using_dma(hsotg) ? (DIEPMSK_XferComplMsk |
2298 DIEPMSK_TimeOUTMsk) : 0) |
2299 DOEPMSK_EPDisbldMsk | DOEPMSK_AHBErrMsk |
2300 DOEPMSK_SetupMsk,
2301 hsotg->regs + DOEPMSK);
2302
2303 writel(0, hsotg->regs + DAINTMSK);
2304
2305 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2306 readl(hsotg->regs + DIEPCTL0),
2307 readl(hsotg->regs + DOEPCTL0));
2308
2309
2310 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPInt | GINTSTS_IEPInt);
2311
2312
2313
2314
2315
2316
2317 if (!using_dma(hsotg))
2318 s3c_hsotg_en_gsint(hsotg, GINTSTS_RxFLvl);
2319
2320
2321 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2322 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2323
2324 __orr32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
2325 udelay(10);
2326 __bic32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
2327
2328 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
2329
2330
2331
2332
2333
2334
2335
2336 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
2337 DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
2338
2339 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2340 DxEPCTL_CNAK | DxEPCTL_EPEna |
2341 DxEPCTL_USBActEp,
2342 hsotg->regs + DOEPCTL0);
2343
2344
2345 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2346 DxEPCTL_USBActEp, hsotg->regs + DIEPCTL0);
2347
2348 s3c_hsotg_enqueue_setup(hsotg);
2349
2350 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2351 readl(hsotg->regs + DIEPCTL0),
2352 readl(hsotg->regs + DOEPCTL0));
2353
2354
2355 writel(DCTL_CGOUTNak | DCTL_CGNPInNAK,
2356 hsotg->regs + DCTL);
2357
2358
2359 mdelay(3);
2360
2361
2362 __bic32(hsotg->regs + DCTL, DCTL_SftDiscon);
2363}
2364
2365
2366
2367
2368
2369
2370static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2371{
2372 struct s3c_hsotg *hsotg = pw;
2373 int retry_count = 8;
2374 u32 gintsts;
2375 u32 gintmsk;
2376
2377 spin_lock(&hsotg->lock);
2378irq_retry:
2379 gintsts = readl(hsotg->regs + GINTSTS);
2380 gintmsk = readl(hsotg->regs + GINTMSK);
2381
2382 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2383 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2384
2385 gintsts &= gintmsk;
2386
2387 if (gintsts & GINTSTS_OTGInt) {
2388 u32 otgint = readl(hsotg->regs + GOTGINT);
2389
2390 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2391
2392 writel(otgint, hsotg->regs + GOTGINT);
2393 }
2394
2395 if (gintsts & GINTSTS_SessReqInt) {
2396 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
2397 writel(GINTSTS_SessReqInt, hsotg->regs + GINTSTS);
2398 }
2399
2400 if (gintsts & GINTSTS_EnumDone) {
2401 writel(GINTSTS_EnumDone, hsotg->regs + GINTSTS);
2402
2403 s3c_hsotg_irq_enumdone(hsotg);
2404 }
2405
2406 if (gintsts & GINTSTS_ConIDStsChng) {
2407 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
2408 readl(hsotg->regs + DSTS),
2409 readl(hsotg->regs + GOTGCTL));
2410
2411 writel(GINTSTS_ConIDStsChng, hsotg->regs + GINTSTS);
2412 }
2413
2414 if (gintsts & (GINTSTS_OEPInt | GINTSTS_IEPInt)) {
2415 u32 daint = readl(hsotg->regs + DAINT);
2416 u32 daint_out = daint >> DAINT_OutEP_SHIFT;
2417 u32 daint_in = daint & ~(daint_out << DAINT_OutEP_SHIFT);
2418 int ep;
2419
2420 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2421
2422 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2423 if (daint_out & 1)
2424 s3c_hsotg_epint(hsotg, ep, 0);
2425 }
2426
2427 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2428 if (daint_in & 1)
2429 s3c_hsotg_epint(hsotg, ep, 1);
2430 }
2431 }
2432
2433 if (gintsts & GINTSTS_USBRst) {
2434
2435 u32 usb_status = readl(hsotg->regs + GOTGCTL);
2436
2437 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2438 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2439 readl(hsotg->regs + GNPTXSTS));
2440
2441 writel(GINTSTS_USBRst, hsotg->regs + GINTSTS);
2442
2443 if (usb_status & GOTGCTL_BSESVLD) {
2444 if (time_after(jiffies, hsotg->last_rst +
2445 msecs_to_jiffies(200))) {
2446
2447 kill_all_requests(hsotg, &hsotg->eps[0],
2448 -ECONNRESET, true);
2449
2450 s3c_hsotg_core_init(hsotg);
2451 hsotg->last_rst = jiffies;
2452 }
2453 }
2454 }
2455
2456
2457
2458 if (gintsts & GINTSTS_NPTxFEmp) {
2459 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2460
2461
2462
2463
2464
2465
2466
2467 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTxFEmp);
2468 s3c_hsotg_irq_fifoempty(hsotg, false);
2469 }
2470
2471 if (gintsts & GINTSTS_PTxFEmp) {
2472 dev_dbg(hsotg->dev, "PTxFEmp\n");
2473
2474
2475
2476 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTxFEmp);
2477 s3c_hsotg_irq_fifoempty(hsotg, true);
2478 }
2479
2480 if (gintsts & GINTSTS_RxFLvl) {
2481
2482
2483
2484
2485
2486
2487 s3c_hsotg_handle_rx(hsotg);
2488 }
2489
2490 if (gintsts & GINTSTS_ModeMis) {
2491 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
2492 writel(GINTSTS_ModeMis, hsotg->regs + GINTSTS);
2493 }
2494
2495 if (gintsts & GINTSTS_USBSusp) {
2496 dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
2497 writel(GINTSTS_USBSusp, hsotg->regs + GINTSTS);
2498
2499 call_gadget(hsotg, suspend);
2500 s3c_hsotg_disconnect(hsotg);
2501 }
2502
2503 if (gintsts & GINTSTS_WkUpInt) {
2504 dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
2505 writel(GINTSTS_WkUpInt, hsotg->regs + GINTSTS);
2506
2507 call_gadget(hsotg, resume);
2508 }
2509
2510 if (gintsts & GINTSTS_ErlySusp) {
2511 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
2512 writel(GINTSTS_ErlySusp, hsotg->regs + GINTSTS);
2513
2514 s3c_hsotg_disconnect(hsotg);
2515 }
2516
2517
2518
2519
2520
2521
2522
2523 if (gintsts & GINTSTS_GOUTNakEff) {
2524 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2525
2526 writel(DCTL_CGOUTNak, hsotg->regs + DCTL);
2527
2528 s3c_hsotg_dump(hsotg);
2529 }
2530
2531 if (gintsts & GINTSTS_GINNakEff) {
2532 dev_info(hsotg->dev, "GINNakEff triggered\n");
2533
2534 writel(DCTL_CGNPInNAK, hsotg->regs + DCTL);
2535
2536 s3c_hsotg_dump(hsotg);
2537 }
2538
2539
2540
2541
2542
2543
2544 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2545 goto irq_retry;
2546
2547 spin_unlock(&hsotg->lock);
2548
2549 return IRQ_HANDLED;
2550}
2551
2552
2553
2554
2555
2556
2557
2558
2559static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2560 const struct usb_endpoint_descriptor *desc)
2561{
2562 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2563 struct s3c_hsotg *hsotg = hs_ep->parent;
2564 unsigned long flags;
2565 int index = hs_ep->index;
2566 u32 epctrl_reg;
2567 u32 epctrl;
2568 u32 mps;
2569 int dir_in;
2570 int ret = 0;
2571
2572 dev_dbg(hsotg->dev,
2573 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2574 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2575 desc->wMaxPacketSize, desc->bInterval);
2576
2577
2578 WARN_ON(index == 0);
2579
2580 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2581 if (dir_in != hs_ep->dir_in) {
2582 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2583 return -EINVAL;
2584 }
2585
2586 mps = usb_endpoint_maxp(desc);
2587
2588
2589
2590 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
2591 epctrl = readl(hsotg->regs + epctrl_reg);
2592
2593 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2594 __func__, epctrl, epctrl_reg);
2595
2596 spin_lock_irqsave(&hsotg->lock, flags);
2597
2598 epctrl &= ~(DxEPCTL_EPType_MASK | DxEPCTL_MPS_MASK);
2599 epctrl |= DxEPCTL_MPS(mps);
2600
2601
2602
2603
2604
2605 epctrl |= DxEPCTL_USBActEp;
2606
2607
2608
2609
2610
2611
2612
2613
2614 epctrl |= DxEPCTL_SNAK;
2615
2616
2617 hs_ep->ep.maxpacket = mps;
2618
2619
2620 hs_ep->periodic = 0;
2621
2622 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2623 case USB_ENDPOINT_XFER_ISOC:
2624 dev_err(hsotg->dev, "no current ISOC support\n");
2625 ret = -EINVAL;
2626 goto out;
2627
2628 case USB_ENDPOINT_XFER_BULK:
2629 epctrl |= DxEPCTL_EPType_Bulk;
2630 break;
2631
2632 case USB_ENDPOINT_XFER_INT:
2633 if (dir_in) {
2634
2635
2636
2637
2638
2639
2640
2641 hs_ep->periodic = 1;
2642 epctrl |= DxEPCTL_TxFNum(index);
2643 }
2644
2645 epctrl |= DxEPCTL_EPType_Intterupt;
2646 break;
2647
2648 case USB_ENDPOINT_XFER_CONTROL:
2649 epctrl |= DxEPCTL_EPType_Control;
2650 break;
2651 }
2652
2653
2654
2655
2656
2657 if (dir_in && hsotg->dedicated_fifos)
2658 epctrl |= DxEPCTL_TxFNum(index);
2659
2660
2661 if (index)
2662 epctrl |= DxEPCTL_SetD0PID;
2663
2664 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2665 __func__, epctrl);
2666
2667 writel(epctrl, hsotg->regs + epctrl_reg);
2668 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2669 __func__, readl(hsotg->regs + epctrl_reg));
2670
2671
2672 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2673
2674out:
2675 spin_unlock_irqrestore(&hsotg->lock, flags);
2676 return ret;
2677}
2678
2679
2680
2681
2682
2683static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2684{
2685 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2686 struct s3c_hsotg *hsotg = hs_ep->parent;
2687 int dir_in = hs_ep->dir_in;
2688 int index = hs_ep->index;
2689 unsigned long flags;
2690 u32 epctrl_reg;
2691 u32 ctrl;
2692
2693 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2694
2695 if (ep == &hsotg->eps[0].ep) {
2696 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2697 return -EINVAL;
2698 }
2699
2700 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
2701
2702 spin_lock_irqsave(&hsotg->lock, flags);
2703
2704 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2705
2706
2707 ctrl = readl(hsotg->regs + epctrl_reg);
2708 ctrl &= ~DxEPCTL_EPEna;
2709 ctrl &= ~DxEPCTL_USBActEp;
2710 ctrl |= DxEPCTL_SNAK;
2711
2712 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2713 writel(ctrl, hsotg->regs + epctrl_reg);
2714
2715
2716 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2717
2718 spin_unlock_irqrestore(&hsotg->lock, flags);
2719 return 0;
2720}
2721
2722
2723
2724
2725
2726
2727static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2728{
2729 struct s3c_hsotg_req *req, *treq;
2730
2731 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2732 if (req == test)
2733 return true;
2734 }
2735
2736 return false;
2737}
2738
2739
2740
2741
2742
2743
2744static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2745{
2746 struct s3c_hsotg_req *hs_req = our_req(req);
2747 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2748 struct s3c_hsotg *hs = hs_ep->parent;
2749 unsigned long flags;
2750
2751 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2752
2753 spin_lock_irqsave(&hs->lock, flags);
2754
2755 if (!on_list(hs_ep, hs_req)) {
2756 spin_unlock_irqrestore(&hs->lock, flags);
2757 return -EINVAL;
2758 }
2759
2760 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
2761 spin_unlock_irqrestore(&hs->lock, flags);
2762
2763 return 0;
2764}
2765
2766
2767
2768
2769
2770
2771static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2772{
2773 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2774 struct s3c_hsotg *hs = hs_ep->parent;
2775 int index = hs_ep->index;
2776 u32 epreg;
2777 u32 epctl;
2778 u32 xfertype;
2779
2780 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2781
2782
2783
2784 epreg = DIEPCTL(index);
2785 epctl = readl(hs->regs + epreg);
2786
2787 if (value) {
2788 epctl |= DxEPCTL_Stall + DxEPCTL_SNAK;
2789 if (epctl & DxEPCTL_EPEna)
2790 epctl |= DxEPCTL_EPDis;
2791 } else {
2792 epctl &= ~DxEPCTL_Stall;
2793 xfertype = epctl & DxEPCTL_EPType_MASK;
2794 if (xfertype == DxEPCTL_EPType_Bulk ||
2795 xfertype == DxEPCTL_EPType_Intterupt)
2796 epctl |= DxEPCTL_SetD0PID;
2797 }
2798
2799 writel(epctl, hs->regs + epreg);
2800
2801 epreg = DOEPCTL(index);
2802 epctl = readl(hs->regs + epreg);
2803
2804 if (value)
2805 epctl |= DxEPCTL_Stall;
2806 else {
2807 epctl &= ~DxEPCTL_Stall;
2808 xfertype = epctl & DxEPCTL_EPType_MASK;
2809 if (xfertype == DxEPCTL_EPType_Bulk ||
2810 xfertype == DxEPCTL_EPType_Intterupt)
2811 epctl |= DxEPCTL_SetD0PID;
2812 }
2813
2814 writel(epctl, hs->regs + epreg);
2815
2816 return 0;
2817}
2818
2819
2820
2821
2822
2823
2824static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2825{
2826 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2827 struct s3c_hsotg *hs = hs_ep->parent;
2828 unsigned long flags = 0;
2829 int ret = 0;
2830
2831 spin_lock_irqsave(&hs->lock, flags);
2832 ret = s3c_hsotg_ep_sethalt(ep, value);
2833 spin_unlock_irqrestore(&hs->lock, flags);
2834
2835 return ret;
2836}
2837
2838static struct usb_ep_ops s3c_hsotg_ep_ops = {
2839 .enable = s3c_hsotg_ep_enable,
2840 .disable = s3c_hsotg_ep_disable,
2841 .alloc_request = s3c_hsotg_ep_alloc_request,
2842 .free_request = s3c_hsotg_ep_free_request,
2843 .queue = s3c_hsotg_ep_queue_lock,
2844 .dequeue = s3c_hsotg_ep_dequeue,
2845 .set_halt = s3c_hsotg_ep_sethalt_lock,
2846
2847};
2848
2849
2850
2851
2852
2853
2854
2855
2856static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2857{
2858 struct platform_device *pdev = to_platform_device(hsotg->dev);
2859
2860 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
2861
2862 if (hsotg->phy)
2863 usb_phy_init(hsotg->phy);
2864 else if (hsotg->plat->phy_init)
2865 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2866}
2867
2868
2869
2870
2871
2872
2873
2874
2875static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2876{
2877 struct platform_device *pdev = to_platform_device(hsotg->dev);
2878
2879 if (hsotg->phy)
2880 usb_phy_shutdown(hsotg->phy);
2881 else if (hsotg->plat->phy_exit)
2882 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2883}
2884
2885
2886
2887
2888
2889static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2890{
2891
2892
2893 writel(DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2894 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk,
2895 hsotg->regs + DIEPMSK);
2896
2897 writel(DOEPMSK_SetupMsk | DOEPMSK_AHBErrMsk |
2898 DOEPMSK_EPDisbldMsk | DOEPMSK_XferComplMsk,
2899 hsotg->regs + DOEPMSK);
2900
2901 writel(0, hsotg->regs + DAINTMSK);
2902
2903
2904 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
2905
2906 if (0) {
2907
2908 writel(DCTL_SGNPInNAK | DCTL_SGOUTNak,
2909 hsotg->regs + DCTL);
2910 }
2911
2912
2913
2914 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2915 readl(hsotg->regs + GRXFSIZ),
2916 readl(hsotg->regs + GNPTXFSIZ));
2917
2918 s3c_hsotg_init_fifo(hsotg);
2919
2920
2921 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | (0x5 << 10),
2922 hsotg->regs + GUSBCFG);
2923
2924 writel(using_dma(hsotg) ? GAHBCFG_DMAEn : 0x0,
2925 hsotg->regs + GAHBCFG);
2926}
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2937 struct usb_gadget_driver *driver)
2938{
2939 struct s3c_hsotg *hsotg = to_hsotg(gadget);
2940 int ret;
2941
2942 if (!hsotg) {
2943 printk(KERN_ERR "%s: called with no device\n", __func__);
2944 return -ENODEV;
2945 }
2946
2947 if (!driver) {
2948 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2949 return -EINVAL;
2950 }
2951
2952 if (driver->max_speed < USB_SPEED_FULL)
2953 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
2954
2955 if (!driver->setup) {
2956 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2957 return -EINVAL;
2958 }
2959
2960 WARN_ON(hsotg->driver);
2961
2962 driver->driver.bus = NULL;
2963 hsotg->driver = driver;
2964 hsotg->gadget.dev.driver = &driver->driver;
2965 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
2966 hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
2967 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2968
2969 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
2970 hsotg->supplies);
2971 if (ret) {
2972 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
2973 goto err;
2974 }
2975
2976 hsotg->last_rst = jiffies;
2977 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
2978 return 0;
2979
2980err:
2981 hsotg->driver = NULL;
2982 hsotg->gadget.dev.driver = NULL;
2983 return ret;
2984}
2985
2986
2987
2988
2989
2990
2991
2992
2993static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
2994 struct usb_gadget_driver *driver)
2995{
2996 struct s3c_hsotg *hsotg = to_hsotg(gadget);
2997 unsigned long flags = 0;
2998 int ep;
2999
3000 if (!hsotg)
3001 return -ENODEV;
3002
3003 if (!driver || driver != hsotg->driver || !driver->unbind)
3004 return -EINVAL;
3005
3006
3007 for (ep = 0; ep < hsotg->num_of_eps; ep++)
3008 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3009
3010 spin_lock_irqsave(&hsotg->lock, flags);
3011
3012 s3c_hsotg_phy_disable(hsotg);
3013 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3014
3015 hsotg->driver = NULL;
3016 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3017 hsotg->gadget.dev.driver = NULL;
3018
3019 spin_unlock_irqrestore(&hsotg->lock, flags);
3020
3021 dev_info(hsotg->dev, "unregistered gadget driver '%s'\n",
3022 driver->driver.name);
3023
3024 return 0;
3025}
3026
3027
3028
3029
3030
3031
3032
3033static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
3034{
3035 return s3c_hsotg_read_frameno(to_hsotg(gadget));
3036}
3037
3038
3039
3040
3041
3042
3043
3044
3045static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
3046{
3047 struct s3c_hsotg *hsotg = to_hsotg(gadget);
3048 unsigned long flags = 0;
3049
3050 dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on);
3051
3052 spin_lock_irqsave(&hsotg->lock, flags);
3053 if (is_on) {
3054 s3c_hsotg_phy_enable(hsotg);
3055 s3c_hsotg_core_init(hsotg);
3056 } else {
3057 s3c_hsotg_disconnect(hsotg);
3058 s3c_hsotg_phy_disable(hsotg);
3059 }
3060
3061 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3062 spin_unlock_irqrestore(&hsotg->lock, flags);
3063
3064 return 0;
3065}
3066
3067static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
3068 .get_frame = s3c_hsotg_gadget_getframe,
3069 .udc_start = s3c_hsotg_udc_start,
3070 .udc_stop = s3c_hsotg_udc_stop,
3071 .pullup = s3c_hsotg_pullup,
3072};
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084static void s3c_hsotg_initep(struct s3c_hsotg *hsotg,
3085 struct s3c_hsotg_ep *hs_ep,
3086 int epnum)
3087{
3088 u32 ptxfifo;
3089 char *dir;
3090
3091 if (epnum == 0)
3092 dir = "";
3093 else if ((epnum % 2) == 0) {
3094 dir = "out";
3095 } else {
3096 dir = "in";
3097 hs_ep->dir_in = 1;
3098 }
3099
3100 hs_ep->index = epnum;
3101
3102 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3103
3104 INIT_LIST_HEAD(&hs_ep->queue);
3105 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3106
3107
3108 if (epnum)
3109 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3110
3111 hs_ep->parent = hsotg;
3112 hs_ep->ep.name = hs_ep->name;
3113 hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT;
3114 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3115
3116
3117
3118
3119
3120
3121
3122 ptxfifo = readl(hsotg->regs + DPTXFSIZn(epnum));
3123 hs_ep->fifo_size = DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
3124
3125
3126
3127
3128
3129
3130 if (using_dma(hsotg)) {
3131 u32 next = DxEPCTL_NextEp((epnum + 1) % 15);
3132 writel(next, hsotg->regs + DIEPCTL(epnum));
3133 writel(next, hsotg->regs + DOEPCTL(epnum));
3134 }
3135}
3136
3137
3138
3139
3140
3141
3142
3143static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
3144{
3145 u32 cfg2, cfg4;
3146
3147
3148 cfg2 = readl(hsotg->regs + 0x48);
3149 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3150
3151 dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
3152
3153 cfg4 = readl(hsotg->regs + 0x50);
3154 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3155
3156 dev_info(hsotg->dev, "%s fifos\n",
3157 hsotg->dedicated_fifos ? "dedicated" : "shared");
3158}
3159
3160
3161
3162
3163
3164static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
3165{
3166#ifdef DEBUG
3167 struct device *dev = hsotg->dev;
3168 void __iomem *regs = hsotg->regs;
3169 u32 val;
3170 int idx;
3171
3172 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
3173 readl(regs + DCFG), readl(regs + DCTL),
3174 readl(regs + DIEPMSK));
3175
3176 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
3177 readl(regs + GAHBCFG), readl(regs + 0x44));
3178
3179 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
3180 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
3181
3182
3183
3184 for (idx = 1; idx <= 15; idx++) {
3185 val = readl(regs + DPTXFSIZn(idx));
3186 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
3187 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3188 val & DPTXFSIZn_DPTxFStAddr_MASK);
3189 }
3190
3191 for (idx = 0; idx < 15; idx++) {
3192 dev_info(dev,
3193 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
3194 readl(regs + DIEPCTL(idx)),
3195 readl(regs + DIEPTSIZ(idx)),
3196 readl(regs + DIEPDMA(idx)));
3197
3198 val = readl(regs + DOEPCTL(idx));
3199 dev_info(dev,
3200 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
3201 idx, readl(regs + DOEPCTL(idx)),
3202 readl(regs + DOEPTSIZ(idx)),
3203 readl(regs + DOEPDMA(idx)));
3204
3205 }
3206
3207 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
3208 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
3209#endif
3210}
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221static int state_show(struct seq_file *seq, void *v)
3222{
3223 struct s3c_hsotg *hsotg = seq->private;
3224 void __iomem *regs = hsotg->regs;
3225 int idx;
3226
3227 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
3228 readl(regs + DCFG),
3229 readl(regs + DCTL),
3230 readl(regs + DSTS));
3231
3232 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
3233 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
3234
3235 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
3236 readl(regs + GINTMSK),
3237 readl(regs + GINTSTS));
3238
3239 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
3240 readl(regs + DAINTMSK),
3241 readl(regs + DAINT));
3242
3243 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
3244 readl(regs + GNPTXSTS),
3245 readl(regs + GRXSTSR));
3246
3247 seq_printf(seq, "\nEndpoint status:\n");
3248
3249 for (idx = 0; idx < 15; idx++) {
3250 u32 in, out;
3251
3252 in = readl(regs + DIEPCTL(idx));
3253 out = readl(regs + DOEPCTL(idx));
3254
3255 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3256 idx, in, out);
3257
3258 in = readl(regs + DIEPTSIZ(idx));
3259 out = readl(regs + DOEPTSIZ(idx));
3260
3261 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3262 in, out);
3263
3264 seq_printf(seq, "\n");
3265 }
3266
3267 return 0;
3268}
3269
3270static int state_open(struct inode *inode, struct file *file)
3271{
3272 return single_open(file, state_show, inode->i_private);
3273}
3274
3275static const struct file_operations state_fops = {
3276 .owner = THIS_MODULE,
3277 .open = state_open,
3278 .read = seq_read,
3279 .llseek = seq_lseek,
3280 .release = single_release,
3281};
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291static int fifo_show(struct seq_file *seq, void *v)
3292{
3293 struct s3c_hsotg *hsotg = seq->private;
3294 void __iomem *regs = hsotg->regs;
3295 u32 val;
3296 int idx;
3297
3298 seq_printf(seq, "Non-periodic FIFOs:\n");
3299 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
3300
3301 val = readl(regs + GNPTXFSIZ);
3302 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
3303 val >> GNPTXFSIZ_NPTxFDep_SHIFT,
3304 val & GNPTXFSIZ_NPTxFStAddr_MASK);
3305
3306 seq_printf(seq, "\nPeriodic TXFIFOs:\n");
3307
3308 for (idx = 1; idx <= 15; idx++) {
3309 val = readl(regs + DPTXFSIZn(idx));
3310
3311 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
3312 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3313 val & DPTXFSIZn_DPTxFStAddr_MASK);
3314 }
3315
3316 return 0;
3317}
3318
3319static int fifo_open(struct inode *inode, struct file *file)
3320{
3321 return single_open(file, fifo_show, inode->i_private);
3322}
3323
3324static const struct file_operations fifo_fops = {
3325 .owner = THIS_MODULE,
3326 .open = fifo_open,
3327 .read = seq_read,
3328 .llseek = seq_lseek,
3329 .release = single_release,
3330};
3331
3332
3333static const char *decode_direction(int is_in)
3334{
3335 return is_in ? "in" : "out";
3336}
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346static int ep_show(struct seq_file *seq, void *v)
3347{
3348 struct s3c_hsotg_ep *ep = seq->private;
3349 struct s3c_hsotg *hsotg = ep->parent;
3350 struct s3c_hsotg_req *req;
3351 void __iomem *regs = hsotg->regs;
3352 int index = ep->index;
3353 int show_limit = 15;
3354 unsigned long flags;
3355
3356 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3357 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3358
3359
3360
3361 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
3362 readl(regs + DIEPCTL(index)),
3363 readl(regs + DOEPCTL(index)));
3364
3365 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
3366 readl(regs + DIEPDMA(index)),
3367 readl(regs + DOEPDMA(index)));
3368
3369 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
3370 readl(regs + DIEPINT(index)),
3371 readl(regs + DOEPINT(index)));
3372
3373 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
3374 readl(regs + DIEPTSIZ(index)),
3375 readl(regs + DOEPTSIZ(index)));
3376
3377 seq_printf(seq, "\n");
3378 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3379 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3380
3381 seq_printf(seq, "request list (%p,%p):\n",
3382 ep->queue.next, ep->queue.prev);
3383
3384 spin_lock_irqsave(&hsotg->lock, flags);
3385
3386 list_for_each_entry(req, &ep->queue, queue) {
3387 if (--show_limit < 0) {
3388 seq_printf(seq, "not showing more requests...\n");
3389 break;
3390 }
3391
3392 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3393 req == ep->req ? '*' : ' ',
3394 req, req->req.length, req->req.buf);
3395 seq_printf(seq, "%d done, res %d\n",
3396 req->req.actual, req->req.status);
3397 }
3398
3399 spin_unlock_irqrestore(&hsotg->lock, flags);
3400
3401 return 0;
3402}
3403
3404static int ep_open(struct inode *inode, struct file *file)
3405{
3406 return single_open(file, ep_show, inode->i_private);
3407}
3408
3409static const struct file_operations ep_fops = {
3410 .owner = THIS_MODULE,
3411 .open = ep_open,
3412 .read = seq_read,
3413 .llseek = seq_lseek,
3414 .release = single_release,
3415};
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3427{
3428 struct dentry *root;
3429 unsigned epidx;
3430
3431 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3432 hsotg->debug_root = root;
3433 if (IS_ERR(root)) {
3434 dev_err(hsotg->dev, "cannot create debug root\n");
3435 return;
3436 }
3437
3438
3439
3440 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3441 hsotg, &state_fops);
3442
3443 if (IS_ERR(hsotg->debug_file))
3444 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3445
3446 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3447 hsotg, &fifo_fops);
3448
3449 if (IS_ERR(hsotg->debug_fifo))
3450 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3451
3452
3453
3454 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
3455 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3456
3457 ep->debugfs = debugfs_create_file(ep->name, 0444,
3458 root, ep, &ep_fops);
3459
3460 if (IS_ERR(ep->debugfs))
3461 dev_err(hsotg->dev, "failed to create %s debug file\n",
3462 ep->name);
3463 }
3464}
3465
3466
3467
3468
3469
3470
3471
3472static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3473{
3474 unsigned epidx;
3475
3476 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
3477 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3478 debugfs_remove(ep->debugfs);
3479 }
3480
3481 debugfs_remove(hsotg->debug_file);
3482 debugfs_remove(hsotg->debug_fifo);
3483 debugfs_remove(hsotg->debug_root);
3484}
3485
3486
3487
3488
3489
3490
3491
3492static void s3c_hsotg_release(struct device *dev)
3493{
3494}
3495
3496
3497
3498
3499
3500
3501static int s3c_hsotg_probe(struct platform_device *pdev)
3502{
3503 struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
3504 struct usb_phy *phy;
3505 struct device *dev = &pdev->dev;
3506 struct s3c_hsotg_ep *eps;
3507 struct s3c_hsotg *hsotg;
3508 struct resource *res;
3509 int epnum;
3510 int ret;
3511 int i;
3512
3513 hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
3514 if (!hsotg) {
3515 dev_err(dev, "cannot get memory\n");
3516 return -ENOMEM;
3517 }
3518
3519 phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3520 if (IS_ERR_OR_NULL(phy)) {
3521
3522 plat = pdev->dev.platform_data;
3523 if (!plat) {
3524 dev_err(&pdev->dev, "no platform data or transceiver defined\n");
3525 return -EPROBE_DEFER;
3526 } else {
3527 hsotg->plat = plat;
3528 }
3529 } else {
3530 hsotg->phy = phy;
3531 }
3532
3533 hsotg->dev = dev;
3534
3535 hsotg->clk = devm_clk_get(&pdev->dev, "otg");
3536 if (IS_ERR(hsotg->clk)) {
3537 dev_err(dev, "cannot get otg clock\n");
3538 return PTR_ERR(hsotg->clk);
3539 }
3540
3541 platform_set_drvdata(pdev, hsotg);
3542
3543 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3544
3545 hsotg->regs = devm_ioremap_resource(&pdev->dev, res);
3546 if (IS_ERR(hsotg->regs)) {
3547 ret = PTR_ERR(hsotg->regs);
3548 goto err_clk;
3549 }
3550
3551 ret = platform_get_irq(pdev, 0);
3552 if (ret < 0) {
3553 dev_err(dev, "cannot find IRQ\n");
3554 goto err_clk;
3555 }
3556
3557 spin_lock_init(&hsotg->lock);
3558
3559 hsotg->irq = ret;
3560
3561 ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
3562 dev_name(dev), hsotg);
3563 if (ret < 0) {
3564 dev_err(dev, "cannot claim IRQ\n");
3565 goto err_clk;
3566 }
3567
3568 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3569
3570 device_initialize(&hsotg->gadget.dev);
3571
3572 dev_set_name(&hsotg->gadget.dev, "gadget");
3573
3574 hsotg->gadget.max_speed = USB_SPEED_HIGH;
3575 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3576 hsotg->gadget.name = dev_name(dev);
3577
3578 hsotg->gadget.dev.parent = dev;
3579 hsotg->gadget.dev.dma_mask = dev->dma_mask;
3580 hsotg->gadget.dev.release = s3c_hsotg_release;
3581
3582
3583
3584 clk_prepare_enable(hsotg->clk);
3585
3586
3587
3588 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3589 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3590
3591 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
3592 hsotg->supplies);
3593 if (ret) {
3594 dev_err(dev, "failed to request supplies: %d\n", ret);
3595 goto err_clk;
3596 }
3597
3598 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3599 hsotg->supplies);
3600
3601 if (ret) {
3602 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3603 goto err_supplies;
3604 }
3605
3606
3607 s3c_hsotg_phy_enable(hsotg);
3608
3609 s3c_hsotg_corereset(hsotg);
3610 s3c_hsotg_init(hsotg);
3611 s3c_hsotg_hw_cfg(hsotg);
3612
3613
3614
3615 if (hsotg->num_of_eps == 0) {
3616 dev_err(dev, "wrong number of EPs (zero)\n");
3617 ret = -EINVAL;
3618 goto err_supplies;
3619 }
3620
3621 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3622 GFP_KERNEL);
3623 if (!eps) {
3624 dev_err(dev, "cannot get memory\n");
3625 ret = -ENOMEM;
3626 goto err_supplies;
3627 }
3628
3629 hsotg->eps = eps;
3630
3631
3632
3633 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3634 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3635
3636
3637
3638 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3639 GFP_KERNEL);
3640 if (!hsotg->ctrl_req) {
3641 dev_err(dev, "failed to allocate ctrl req\n");
3642 ret = -ENOMEM;
3643 goto err_ep_mem;
3644 }
3645
3646
3647 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
3648 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3649
3650
3651
3652 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3653 hsotg->supplies);
3654 if (ret) {
3655 dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
3656 goto err_ep_mem;
3657 }
3658
3659 s3c_hsotg_phy_disable(hsotg);
3660
3661 ret = device_add(&hsotg->gadget.dev);
3662 if (ret) {
3663 put_device(&hsotg->gadget.dev);
3664 goto err_ep_mem;
3665 }
3666
3667 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3668 if (ret)
3669 goto err_ep_mem;
3670
3671 s3c_hsotg_create_debug(hsotg);
3672
3673 s3c_hsotg_dump(hsotg);
3674
3675 return 0;
3676
3677err_ep_mem:
3678 kfree(eps);
3679err_supplies:
3680 s3c_hsotg_phy_disable(hsotg);
3681err_clk:
3682 clk_disable_unprepare(hsotg->clk);
3683
3684 return ret;
3685}
3686
3687
3688
3689
3690
3691static int s3c_hsotg_remove(struct platform_device *pdev)
3692{
3693 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3694
3695 usb_del_gadget_udc(&hsotg->gadget);
3696
3697 s3c_hsotg_delete_debug(hsotg);
3698
3699 if (hsotg->driver) {
3700
3701 usb_gadget_unregister_driver(hsotg->driver);
3702 }
3703
3704 s3c_hsotg_phy_disable(hsotg);
3705
3706 clk_disable_unprepare(hsotg->clk);
3707
3708 device_unregister(&hsotg->gadget.dev);
3709 return 0;
3710}
3711
3712#if 1
3713#define s3c_hsotg_suspend NULL
3714#define s3c_hsotg_resume NULL
3715#endif
3716
3717static struct platform_driver s3c_hsotg_driver = {
3718 .driver = {
3719 .name = "s3c-hsotg",
3720 .owner = THIS_MODULE,
3721 },
3722 .probe = s3c_hsotg_probe,
3723 .remove = s3c_hsotg_remove,
3724 .suspend = s3c_hsotg_suspend,
3725 .resume = s3c_hsotg_resume,
3726};
3727
3728module_platform_driver(s3c_hsotg_driver);
3729
3730MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3731MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3732MODULE_LICENSE("GPL");
3733MODULE_ALIAS("platform:s3c-hsotg");
3734