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#include <linux/usb.h>
49#include <linux/slab.h>
50#include <linux/module.h>
51#include <linux/usb/hcd.h>
52#include <linux/prefetch.h>
53#include <linux/dma-mapping.h>
54#include <linux/platform_device.h>
55
56#include <asm/octeon/octeon.h>
57
58#include "octeon-hcd.h"
59
60
61
62
63
64
65
66
67enum cvmx_usb_speed {
68 CVMX_USB_SPEED_HIGH = 0,
69 CVMX_USB_SPEED_FULL = 1,
70 CVMX_USB_SPEED_LOW = 2,
71};
72
73
74
75
76
77
78
79
80
81
82
83
84
85enum cvmx_usb_transfer {
86 CVMX_USB_TRANSFER_CONTROL = 0,
87 CVMX_USB_TRANSFER_ISOCHRONOUS = 1,
88 CVMX_USB_TRANSFER_BULK = 2,
89 CVMX_USB_TRANSFER_INTERRUPT = 3,
90};
91
92
93
94
95
96
97
98enum cvmx_usb_direction {
99 CVMX_USB_DIRECTION_OUT,
100 CVMX_USB_DIRECTION_IN,
101};
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123enum cvmx_usb_status {
124 CVMX_USB_STATUS_OK,
125 CVMX_USB_STATUS_SHORT,
126 CVMX_USB_STATUS_CANCEL,
127 CVMX_USB_STATUS_ERROR,
128 CVMX_USB_STATUS_STALL,
129 CVMX_USB_STATUS_XACTERR,
130 CVMX_USB_STATUS_DATATGLERR,
131 CVMX_USB_STATUS_BABBLEERR,
132 CVMX_USB_STATUS_FRAMEERR,
133};
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150struct cvmx_usb_port_status {
151 u32 reserved : 25;
152 u32 port_enabled : 1;
153 u32 port_over_current : 1;
154 u32 port_powered : 1;
155 enum cvmx_usb_speed port_speed : 2;
156 u32 connected : 1;
157 u32 connect_change : 1;
158};
159
160
161
162
163
164
165
166
167
168struct cvmx_usb_iso_packet {
169 int offset;
170 int length;
171 enum cvmx_usb_status status;
172};
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191enum cvmx_usb_initialize_flags {
192 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI = 1 << 0,
193 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND = 1 << 1,
194 CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK = 3 << 3,
195 CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ = 1 << 3,
196 CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ = 2 << 3,
197 CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ = 3 << 3,
198
199 CVMX_USB_INITIALIZE_FLAGS_NO_DMA = 1 << 5,
200};
201
202
203
204
205
206
207
208
209
210enum cvmx_usb_pipe_flags {
211 CVMX_USB_PIPE_FLAGS_SCHEDULED = 1 << 17,
212 CVMX_USB_PIPE_FLAGS_NEED_PING = 1 << 18,
213};
214
215
216#define MAX_RETRIES 3
217
218
219#define MAX_CHANNELS 8
220
221
222
223
224
225#define MAX_TRANSFER_BYTES ((1 << 19) - 1)
226
227
228
229
230
231#define MAX_TRANSFER_PACKETS ((1 << 10) - 1)
232
233
234
235
236
237
238
239
240
241enum cvmx_usb_stage {
242 CVMX_USB_STAGE_NON_CONTROL,
243 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
244 CVMX_USB_STAGE_SETUP,
245 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
246 CVMX_USB_STAGE_DATA,
247 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
248 CVMX_USB_STAGE_STATUS,
249 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
250};
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272struct cvmx_usb_transaction {
273 struct list_head node;
274 enum cvmx_usb_transfer type;
275 u64 buffer;
276 int buffer_length;
277 u64 control_header;
278 int iso_start_frame;
279 int iso_number_packets;
280 struct cvmx_usb_iso_packet *iso_packets;
281 int xfersize;
282 int pktcnt;
283 int retries;
284 int actual_bytes;
285 enum cvmx_usb_stage stage;
286 struct urb *urb;
287};
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316struct cvmx_usb_pipe {
317 struct list_head node;
318 struct list_head transactions;
319 u64 interval;
320 u64 next_tx_frame;
321 enum cvmx_usb_pipe_flags flags;
322 enum cvmx_usb_speed device_speed;
323 enum cvmx_usb_transfer transfer_type;
324 enum cvmx_usb_direction transfer_dir;
325 int multi_count;
326 u16 max_packet;
327 u8 device_addr;
328 u8 endpoint_num;
329 u8 hub_device_addr;
330 u8 hub_port;
331 u8 pid_toggle;
332 u8 channel;
333 s8 split_sc_frame;
334};
335
336struct cvmx_usb_tx_fifo {
337 struct {
338 int channel;
339 int size;
340 u64 address;
341 } entry[MAX_CHANNELS + 1];
342 int head;
343 int tail;
344};
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364struct octeon_hcd {
365 spinlock_t lock;
366 int init_flags;
367 int index;
368 int idle_hardware_channels;
369 union cvmx_usbcx_hprt usbcx_hprt;
370 struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
371 int indent;
372 struct cvmx_usb_port_status port_status;
373 struct list_head idle_pipes;
374 struct list_head active_pipes[4];
375 u64 frame_number;
376 struct cvmx_usb_transaction *active_split;
377 struct cvmx_usb_tx_fifo periodic;
378 struct cvmx_usb_tx_fifo nonperiodic;
379};
380
381
382
383
384
385#define USB_SET_FIELD32(address, _union, field, value) \
386 do { \
387 union _union c; \
388 \
389 c.u32 = cvmx_usb_read_csr32(usb, address); \
390 c.s.field = value; \
391 cvmx_usb_write_csr32(usb, address, c.u32); \
392 } while (0)
393
394
395#define USB_FIFO_ADDRESS(channel, usb_index) \
396 (CVMX_USBCX_GOTGCTL(usb_index) + ((channel) + 1) * 0x1000)
397
398
399
400
401
402
403
404
405
406
407struct octeon_temp_buffer {
408 void *orig_buffer;
409 u8 data[];
410};
411
412static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
413{
414 return container_of((void *)p, struct usb_hcd, hcd_priv);
415}
416
417
418
419
420
421
422
423
424
425
426static int octeon_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
427{
428 struct octeon_temp_buffer *temp;
429
430 if (urb->num_sgs || urb->sg ||
431 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
432 !(urb->transfer_buffer_length % sizeof(u32)))
433 return 0;
434
435 temp = kmalloc(ALIGN(urb->transfer_buffer_length, sizeof(u32)) +
436 sizeof(*temp), mem_flags);
437 if (!temp)
438 return -ENOMEM;
439
440 temp->orig_buffer = urb->transfer_buffer;
441 if (usb_urb_dir_out(urb))
442 memcpy(temp->data, urb->transfer_buffer,
443 urb->transfer_buffer_length);
444 urb->transfer_buffer = temp->data;
445 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
446
447 return 0;
448}
449
450
451
452
453
454
455
456static void octeon_free_temp_buffer(struct urb *urb)
457{
458 struct octeon_temp_buffer *temp;
459 size_t length;
460
461 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
462 return;
463
464 temp = container_of(urb->transfer_buffer, struct octeon_temp_buffer,
465 data);
466 if (usb_urb_dir_in(urb)) {
467 if (usb_pipeisoc(urb->pipe))
468 length = urb->transfer_buffer_length;
469 else
470 length = urb->actual_length;
471
472 memcpy(temp->orig_buffer, urb->transfer_buffer, length);
473 }
474 urb->transfer_buffer = temp->orig_buffer;
475 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
476 kfree(temp);
477}
478
479
480
481
482
483
484
485static int octeon_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
486 gfp_t mem_flags)
487{
488 int ret;
489
490 ret = octeon_alloc_temp_buffer(urb, mem_flags);
491 if (ret)
492 return ret;
493
494 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
495 if (ret)
496 octeon_free_temp_buffer(urb);
497
498 return ret;
499}
500
501
502
503
504
505
506static void octeon_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
507{
508 usb_hcd_unmap_urb_for_dma(hcd, urb);
509 octeon_free_temp_buffer(urb);
510}
511
512
513
514
515
516
517
518
519
520
521
522static inline u32 cvmx_usb_read_csr32(struct octeon_hcd *usb, u64 address)
523{
524 return cvmx_read64_uint32(address ^ 4);
525}
526
527
528
529
530
531
532
533
534
535
536static inline void cvmx_usb_write_csr32(struct octeon_hcd *usb,
537 u64 address, u32 value)
538{
539 cvmx_write64_uint32(address ^ 4, value);
540 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
541}
542
543
544
545
546
547
548
549
550
551
552static inline int cvmx_usb_pipe_needs_split(struct octeon_hcd *usb,
553 struct cvmx_usb_pipe *pipe)
554{
555 return pipe->device_speed != CVMX_USB_SPEED_HIGH &&
556 usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH;
557}
558
559
560
561
562
563
564
565
566static inline int cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
567{
568 if (pipe->pid_toggle)
569 return 2;
570 return 0;
571}
572
573
574static int cvmx_wait_tx_rx(struct octeon_hcd *usb, int fflsh_type)
575{
576 int result;
577 u64 address = CVMX_USBCX_GRSTCTL(usb->index);
578 u64 done = cvmx_get_cycle() + 100 *
579 (u64)octeon_get_clock_rate / 1000000;
580 union cvmx_usbcx_grstctl c;
581
582 while (1) {
583 c.u32 = cvmx_usb_read_csr32(usb, address);
584 if (fflsh_type == 0 && c.s.txfflsh == 0) {
585 result = 0;
586 break;
587 } else if (fflsh_type == 1 && c.s.rxfflsh == 0) {
588 result = 0;
589 break;
590 } else if (cvmx_get_cycle() > done) {
591 result = -1;
592 break;
593 }
594
595 __delay(100);
596 }
597 return result;
598}
599
600static void cvmx_fifo_setup(struct octeon_hcd *usb)
601{
602 union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
603 union cvmx_usbcx_gnptxfsiz npsiz;
604 union cvmx_usbcx_hptxfsiz psiz;
605
606 usbcx_ghwcfg3.u32 = cvmx_usb_read_csr32(usb,
607 CVMX_USBCX_GHWCFG3(usb->index));
608
609
610
611
612
613 USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index), cvmx_usbcx_grxfsiz,
614 rxfdep, usbcx_ghwcfg3.s.dfifodepth / 4);
615
616
617
618
619
620
621 npsiz.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index));
622 npsiz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
623 npsiz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
624 cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index), npsiz.u32);
625
626
627
628
629
630
631 psiz.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index));
632 psiz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
633 psiz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
634 cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index), psiz.u32);
635
636
637 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
638 cvmx_usbcx_grstctl, txfnum, 0x10);
639 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
640 cvmx_usbcx_grstctl, txfflsh, 1);
641 cvmx_wait_tx_rx(usb, 0);
642 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
643 cvmx_usbcx_grstctl, rxfflsh, 1);
644 cvmx_wait_tx_rx(usb, 1);
645}
646
647
648
649
650
651
652
653
654
655
656static int cvmx_usb_shutdown(struct octeon_hcd *usb)
657{
658 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
659
660
661 if (!list_empty(&usb->idle_pipes) ||
662 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS]) ||
663 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT]) ||
664 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_CONTROL]) ||
665 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_BULK]))
666 return -EBUSY;
667
668
669 usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
670 usbn_clk_ctl.s.enable = 1;
671 usbn_clk_ctl.s.por = 1;
672 usbn_clk_ctl.s.hclk_rst = 1;
673 usbn_clk_ctl.s.prst = 0;
674 usbn_clk_ctl.s.hrst = 0;
675 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
676 return 0;
677}
678
679
680
681
682
683
684
685
686
687
688
689static int cvmx_usb_initialize(struct device *dev,
690 struct octeon_hcd *usb)
691{
692 int channel;
693 int divisor;
694 int retries = 0;
695 union cvmx_usbcx_hcfg usbcx_hcfg;
696 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
697 union cvmx_usbcx_gintsts usbc_gintsts;
698 union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
699 union cvmx_usbcx_gintmsk usbcx_gintmsk;
700 union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
701 union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
702
703retry:
704
705
706
707
708
709
710
711
712 usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
713 usbn_clk_ctl.s.por = 1;
714 usbn_clk_ctl.s.hrst = 0;
715 usbn_clk_ctl.s.prst = 0;
716 usbn_clk_ctl.s.hclk_rst = 0;
717 usbn_clk_ctl.s.enable = 0;
718
719
720
721
722 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
723
724
725
726
727
728 if (OCTEON_IS_MODEL(OCTEON_CN3XXX) ||
729 OCTEON_IS_MODEL(OCTEON_CN56XX) ||
730 OCTEON_IS_MODEL(OCTEON_CN50XX))
731
732 usbn_clk_ctl.s.p_rtype = 2;
733 else
734
735 usbn_clk_ctl.s.p_rtype = 1;
736
737 switch (usb->init_flags &
738 CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
739 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
740 usbn_clk_ctl.s.p_c_sel = 0;
741 break;
742 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
743 usbn_clk_ctl.s.p_c_sel = 1;
744 break;
745 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
746 usbn_clk_ctl.s.p_c_sel = 2;
747 break;
748 }
749 } else {
750
751
752
753
754 if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
755
756 usbn_clk_ctl.s.p_rtype = 3;
757 else
758
759 usbn_clk_ctl.s.p_rtype = 0;
760
761 usbn_clk_ctl.s.p_c_sel = 0;
762 }
763
764
765
766
767
768 divisor = DIV_ROUND_UP(octeon_get_clock_rate(), 125000000);
769
770 if (divisor < 4)
771 divisor = 4;
772 usbn_clk_ctl.s.divide = divisor;
773 usbn_clk_ctl.s.divide2 = 0;
774 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
775
776
777 usbn_clk_ctl.s.hclk_rst = 1;
778 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
779
780 __delay(64);
781
782
783
784
785
786 usbn_clk_ctl.s.por = 0;
787 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
788
789 mdelay(1);
790
791
792
793
794
795 usbn_usbp_ctl_status.u64 =
796 cvmx_read64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index));
797 usbn_usbp_ctl_status.s.ate_reset = 1;
798 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
799 usbn_usbp_ctl_status.u64);
800
801 __delay(10);
802
803
804
805
806 usbn_usbp_ctl_status.s.ate_reset = 0;
807 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
808 usbn_usbp_ctl_status.u64);
809
810
811
812
813 usbn_clk_ctl.s.prst = 1;
814 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
815
816
817
818
819
820 usbn_usbp_ctl_status.s.hst_mode = 0;
821 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
822 usbn_usbp_ctl_status.u64);
823
824 udelay(1);
825
826
827
828
829 usbn_clk_ctl.s.hrst = 1;
830 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
831
832 usbn_clk_ctl.s.enable = 1;
833 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
834 udelay(1);
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854 usbcx_gahbcfg.u32 = 0;
855 usbcx_gahbcfg.s.dmaen = !(usb->init_flags &
856 CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
857 usbcx_gahbcfg.s.hbstlen = 0;
858 usbcx_gahbcfg.s.nptxfemplvl = 1;
859 usbcx_gahbcfg.s.ptxfemplvl = 1;
860 usbcx_gahbcfg.s.glblintrmsk = 1;
861 cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
862 usbcx_gahbcfg.u32);
863
864
865
866
867
868
869
870
871 usbcx_gusbcfg.u32 = cvmx_usb_read_csr32(usb,
872 CVMX_USBCX_GUSBCFG(usb->index));
873 usbcx_gusbcfg.s.toutcal = 0;
874 usbcx_gusbcfg.s.ddrsel = 0;
875 usbcx_gusbcfg.s.usbtrdtim = 0x5;
876 usbcx_gusbcfg.s.phylpwrclksel = 0;
877 cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
878 usbcx_gusbcfg.u32);
879
880
881
882
883
884
885
886 usbcx_gintmsk.u32 = cvmx_usb_read_csr32(usb,
887 CVMX_USBCX_GINTMSK(usb->index));
888 usbcx_gintmsk.s.otgintmsk = 1;
889 usbcx_gintmsk.s.modemismsk = 1;
890 usbcx_gintmsk.s.hchintmsk = 1;
891 usbcx_gintmsk.s.sofmsk = 0;
892
893 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
894 usbcx_gintmsk.s.rxflvlmsk = 1;
895 cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
896 usbcx_gintmsk.u32);
897
898
899
900
901 for (channel = 0; channel < 8; channel++)
902 cvmx_usb_write_csr32(usb,
903 CVMX_USBCX_HCINTMSKX(channel, usb->index),
904 0);
905
906
907
908
909
910
911
912 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
913 cvmx_usbcx_gintmsk, prtintmsk, 1);
914 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
915 cvmx_usbcx_gintmsk, disconnintmsk, 1);
916
917
918
919
920
921 usbcx_hcfg.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HCFG(usb->index));
922 usbcx_hcfg.s.fslssupp = 0;
923 usbcx_hcfg.s.fslspclksel = 0;
924 cvmx_usb_write_csr32(usb, CVMX_USBCX_HCFG(usb->index), usbcx_hcfg.u32);
925
926 cvmx_fifo_setup(usb);
927
928
929
930
931
932
933 usbc_gintsts.u32 = cvmx_usb_read_csr32(usb,
934 CVMX_USBCX_GINTSTS(usb->index));
935 cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index),
936 usbc_gintsts.u32);
937 dev_dbg(dev, "gintsts after reset: 0x%x\n", (int)usbc_gintsts.u32);
938 if (!usbc_gintsts.s.disconnint && !usbc_gintsts.s.prtint)
939 return 0;
940 if (retries++ >= 5)
941 return -EAGAIN;
942 dev_info(dev, "controller reset failed (gintsts=0x%x) - retrying\n",
943 (int)usbc_gintsts.u32);
944 msleep(50);
945 cvmx_usb_shutdown(usb);
946 msleep(50);
947 goto retry;
948}
949
950
951
952
953
954
955
956static void cvmx_usb_reset_port(struct octeon_hcd *usb)
957{
958 usb->usbcx_hprt.u32 = cvmx_usb_read_csr32(usb,
959 CVMX_USBCX_HPRT(usb->index));
960
961
962 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
963 prtrst, 1);
964
965
966
967
968
969 mdelay(50);
970
971
972 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
973 prtrst, 0);
974
975
976
977
978
979 usb->usbcx_hprt.u32 = cvmx_usb_read_csr32(usb,
980 CVMX_USBCX_HPRT(usb->index));
981}
982
983
984
985
986
987
988
989
990
991
992
993static int cvmx_usb_disable(struct octeon_hcd *usb)
994{
995
996 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
997 prtena, 1);
998 return 0;
999}
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012static struct cvmx_usb_port_status cvmx_usb_get_status(struct octeon_hcd *usb)
1013{
1014 union cvmx_usbcx_hprt usbc_hprt;
1015 struct cvmx_usb_port_status result;
1016
1017 memset(&result, 0, sizeof(result));
1018
1019 usbc_hprt.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1020 result.port_enabled = usbc_hprt.s.prtena;
1021 result.port_over_current = usbc_hprt.s.prtovrcurract;
1022 result.port_powered = usbc_hprt.s.prtpwr;
1023 result.port_speed = usbc_hprt.s.prtspd;
1024 result.connected = usbc_hprt.s.prtconnsts;
1025 result.connect_change =
1026 result.connected != usb->port_status.connected;
1027
1028 return result;
1029}
1030
1031
1032
1033
1034
1035
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
1082
1083static struct cvmx_usb_pipe *cvmx_usb_open_pipe(struct octeon_hcd *usb,
1084 int device_addr,
1085 int endpoint_num,
1086 enum cvmx_usb_speed
1087 device_speed,
1088 int max_packet,
1089 enum cvmx_usb_transfer
1090 transfer_type,
1091 enum cvmx_usb_direction
1092 transfer_dir,
1093 int interval, int multi_count,
1094 int hub_device_addr,
1095 int hub_port)
1096{
1097 struct cvmx_usb_pipe *pipe;
1098
1099 pipe = kzalloc(sizeof(*pipe), GFP_ATOMIC);
1100 if (!pipe)
1101 return NULL;
1102 if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1103 (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1104 (transfer_type == CVMX_USB_TRANSFER_BULK))
1105 pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
1106 pipe->device_addr = device_addr;
1107 pipe->endpoint_num = endpoint_num;
1108 pipe->device_speed = device_speed;
1109 pipe->max_packet = max_packet;
1110 pipe->transfer_type = transfer_type;
1111 pipe->transfer_dir = transfer_dir;
1112 INIT_LIST_HEAD(&pipe->transactions);
1113
1114
1115
1116
1117
1118 if (!interval)
1119 interval = 1;
1120 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1121 pipe->interval = interval * 8;
1122
1123 pipe->next_tx_frame = ((usb->frame_number + 7) & ~7) +
1124 pipe->interval;
1125 } else {
1126 pipe->interval = interval;
1127 pipe->next_tx_frame = usb->frame_number + pipe->interval;
1128 }
1129 pipe->multi_count = multi_count;
1130 pipe->hub_device_addr = hub_device_addr;
1131 pipe->hub_port = hub_port;
1132 pipe->pid_toggle = 0;
1133 pipe->split_sc_frame = -1;
1134 list_add_tail(&pipe->node, &usb->idle_pipes);
1135
1136
1137
1138
1139
1140
1141 return pipe;
1142}
1143
1144
1145
1146
1147
1148
1149
1150
1151static void cvmx_usb_poll_rx_fifo(struct octeon_hcd *usb)
1152{
1153 union cvmx_usbcx_grxstsph rx_status;
1154 int channel;
1155 int bytes;
1156 u64 address;
1157 u32 *ptr;
1158
1159 rx_status.u32 = cvmx_usb_read_csr32(usb,
1160 CVMX_USBCX_GRXSTSPH(usb->index));
1161
1162 if (rx_status.s.pktsts != 2)
1163 return;
1164
1165 if (!rx_status.s.bcnt)
1166 return;
1167
1168 channel = rx_status.s.chnum;
1169 bytes = rx_status.s.bcnt;
1170 if (!bytes)
1171 return;
1172
1173
1174 address = cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) +
1175 channel * 8);
1176
1177 ptr = cvmx_phys_to_ptr(address);
1178 cvmx_write64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel * 8,
1179 address + bytes);
1180
1181
1182 while (bytes > 0) {
1183 *ptr++ = cvmx_usb_read_csr32(usb,
1184 USB_FIFO_ADDRESS(channel, usb->index));
1185 bytes -= 4;
1186 }
1187 CVMX_SYNCW;
1188}
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201static int cvmx_usb_fill_tx_hw(struct octeon_hcd *usb,
1202 struct cvmx_usb_tx_fifo *fifo, int available)
1203{
1204
1205
1206
1207
1208 while (available && (fifo->head != fifo->tail)) {
1209 int i = fifo->tail;
1210 const u32 *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
1211 u64 csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel,
1212 usb->index) ^ 4;
1213 int words = available;
1214
1215
1216 if (fifo->entry[i].size <= available) {
1217 words = fifo->entry[i].size;
1218 fifo->tail++;
1219 if (fifo->tail > MAX_CHANNELS)
1220 fifo->tail = 0;
1221 }
1222
1223
1224 available -= words;
1225 fifo->entry[i].address += words * 4;
1226 fifo->entry[i].size -= words;
1227
1228
1229
1230
1231
1232 while (words > 3) {
1233 cvmx_write64_uint32(csr_address, *ptr++);
1234 cvmx_write64_uint32(csr_address, *ptr++);
1235 cvmx_write64_uint32(csr_address, *ptr++);
1236 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1237 words -= 3;
1238 }
1239 cvmx_write64_uint32(csr_address, *ptr++);
1240 if (--words) {
1241 cvmx_write64_uint32(csr_address, *ptr++);
1242 if (--words)
1243 cvmx_write64_uint32(csr_address, *ptr++);
1244 }
1245 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1246 }
1247 return fifo->head != fifo->tail;
1248}
1249
1250
1251
1252
1253
1254
1255static void cvmx_usb_poll_tx_fifo(struct octeon_hcd *usb)
1256{
1257 if (usb->periodic.head != usb->periodic.tail) {
1258 union cvmx_usbcx_hptxsts tx_status;
1259
1260 tx_status.u32 = cvmx_usb_read_csr32(usb,
1261 CVMX_USBCX_HPTXSTS(usb->index));
1262 if (cvmx_usb_fill_tx_hw(usb, &usb->periodic,
1263 tx_status.s.ptxfspcavail))
1264 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1265 cvmx_usbcx_gintmsk, ptxfempmsk, 1);
1266 else
1267 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1268 cvmx_usbcx_gintmsk, ptxfempmsk, 0);
1269 }
1270
1271 if (usb->nonperiodic.head != usb->nonperiodic.tail) {
1272 union cvmx_usbcx_gnptxsts tx_status;
1273
1274 tx_status.u32 = cvmx_usb_read_csr32(usb,
1275 CVMX_USBCX_GNPTXSTS(usb->index));
1276 if (cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic,
1277 tx_status.s.nptxfspcavail))
1278 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1279 cvmx_usbcx_gintmsk, nptxfempmsk, 1);
1280 else
1281 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1282 cvmx_usbcx_gintmsk, nptxfempmsk, 0);
1283 }
1284}
1285
1286
1287
1288
1289
1290
1291
1292static void cvmx_usb_fill_tx_fifo(struct octeon_hcd *usb, int channel)
1293{
1294 union cvmx_usbcx_hccharx hcchar;
1295 union cvmx_usbcx_hcspltx usbc_hcsplt;
1296 union cvmx_usbcx_hctsizx usbc_hctsiz;
1297 struct cvmx_usb_tx_fifo *fifo;
1298
1299
1300 hcchar.u32 = cvmx_usb_read_csr32(usb,
1301 CVMX_USBCX_HCCHARX(channel, usb->index));
1302 if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1303 return;
1304
1305
1306 usbc_hcsplt.u32 = cvmx_usb_read_csr32(usb,
1307 CVMX_USBCX_HCSPLTX(channel, usb->index));
1308 if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1309 return;
1310
1311
1312
1313
1314
1315 usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
1316 CVMX_USBCX_HCTSIZX(channel, usb->index));
1317 if (!usbc_hctsiz.s.xfersize)
1318 return;
1319
1320 if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1321 (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1322 fifo = &usb->periodic;
1323 else
1324 fifo = &usb->nonperiodic;
1325
1326 fifo->entry[fifo->head].channel = channel;
1327 fifo->entry[fifo->head].address =
1328 cvmx_read64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1329 channel * 8);
1330 fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize + 3) >> 2;
1331 fifo->head++;
1332 if (fifo->head > MAX_CHANNELS)
1333 fifo->head = 0;
1334
1335 cvmx_usb_poll_tx_fifo(usb);
1336}
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346static void cvmx_usb_start_channel_control(struct octeon_hcd *usb,
1347 int channel,
1348 struct cvmx_usb_pipe *pipe)
1349{
1350 struct usb_hcd *hcd = octeon_to_hcd(usb);
1351 struct device *dev = hcd->self.controller;
1352 struct cvmx_usb_transaction *transaction =
1353 list_first_entry(&pipe->transactions, typeof(*transaction),
1354 node);
1355 struct usb_ctrlrequest *header =
1356 cvmx_phys_to_ptr(transaction->control_header);
1357 int bytes_to_transfer = transaction->buffer_length -
1358 transaction->actual_bytes;
1359 int packets_to_transfer;
1360 union cvmx_usbcx_hctsizx usbc_hctsiz;
1361
1362 usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
1363 CVMX_USBCX_HCTSIZX(channel, usb->index));
1364
1365 switch (transaction->stage) {
1366 case CVMX_USB_STAGE_NON_CONTROL:
1367 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
1368 dev_err(dev, "%s: ERROR - Non control stage\n", __func__);
1369 break;
1370 case CVMX_USB_STAGE_SETUP:
1371 usbc_hctsiz.s.pid = 3;
1372 bytes_to_transfer = sizeof(*header);
1373
1374 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1375 cvmx_usbcx_hccharx, epdir,
1376 CVMX_USB_DIRECTION_OUT);
1377
1378
1379
1380
1381 cvmx_write64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1382 channel * 8,
1383 transaction->control_header);
1384 break;
1385 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1386 usbc_hctsiz.s.pid = 3;
1387 bytes_to_transfer = 0;
1388
1389 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1390 cvmx_usbcx_hccharx, epdir,
1391 CVMX_USB_DIRECTION_OUT);
1392
1393 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1394 cvmx_usbcx_hcspltx, compsplt, 1);
1395 break;
1396 case CVMX_USB_STAGE_DATA:
1397 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1398 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1399 if (header->bRequestType & USB_DIR_IN)
1400 bytes_to_transfer = 0;
1401 else if (bytes_to_transfer > pipe->max_packet)
1402 bytes_to_transfer = pipe->max_packet;
1403 }
1404 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1405 cvmx_usbcx_hccharx, epdir,
1406 ((header->bRequestType & USB_DIR_IN) ?
1407 CVMX_USB_DIRECTION_IN :
1408 CVMX_USB_DIRECTION_OUT));
1409 break;
1410 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1411 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1412 if (!(header->bRequestType & USB_DIR_IN))
1413 bytes_to_transfer = 0;
1414 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1415 cvmx_usbcx_hccharx, epdir,
1416 ((header->bRequestType & USB_DIR_IN) ?
1417 CVMX_USB_DIRECTION_IN :
1418 CVMX_USB_DIRECTION_OUT));
1419 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1420 cvmx_usbcx_hcspltx, compsplt, 1);
1421 break;
1422 case CVMX_USB_STAGE_STATUS:
1423 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1424 bytes_to_transfer = 0;
1425 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1426 cvmx_usbcx_hccharx, epdir,
1427 ((header->bRequestType & USB_DIR_IN) ?
1428 CVMX_USB_DIRECTION_OUT :
1429 CVMX_USB_DIRECTION_IN));
1430 break;
1431 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1432 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1433 bytes_to_transfer = 0;
1434 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1435 cvmx_usbcx_hccharx, epdir,
1436 ((header->bRequestType & USB_DIR_IN) ?
1437 CVMX_USB_DIRECTION_OUT :
1438 CVMX_USB_DIRECTION_IN));
1439 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1440 cvmx_usbcx_hcspltx, compsplt, 1);
1441 break;
1442 }
1443
1444
1445
1446
1447
1448 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1449
1450 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1451 bytes_to_transfer *= pipe->max_packet;
1452 }
1453
1454
1455
1456
1457
1458 packets_to_transfer = DIV_ROUND_UP(bytes_to_transfer,
1459 pipe->max_packet);
1460 if (packets_to_transfer == 0) {
1461 packets_to_transfer = 1;
1462 } else if ((packets_to_transfer > 1) &&
1463 (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1464
1465
1466
1467
1468
1469 packets_to_transfer = 1;
1470 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1471 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1472
1473
1474
1475
1476 packets_to_transfer = MAX_TRANSFER_PACKETS;
1477 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1478 }
1479
1480 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1481 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1482
1483 cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index),
1484 usbc_hctsiz.u32);
1485}
1486
1487
1488
1489
1490
1491
1492
1493
1494static void cvmx_usb_start_channel(struct octeon_hcd *usb, int channel,
1495 struct cvmx_usb_pipe *pipe)
1496{
1497 struct cvmx_usb_transaction *transaction =
1498 list_first_entry(&pipe->transactions, typeof(*transaction),
1499 node);
1500
1501
1502 CVMX_SYNCW;
1503
1504
1505 usb->pipe_for_channel[channel] = pipe;
1506 pipe->channel = channel;
1507 pipe->flags |= CVMX_USB_PIPE_FLAGS_SCHEDULED;
1508
1509
1510 usb->idle_hardware_channels &= ~(1 << channel);
1511
1512
1513 {
1514 union cvmx_usbcx_hcintx usbc_hcint;
1515 union cvmx_usbcx_hcintmskx usbc_hcintmsk;
1516 union cvmx_usbcx_haintmsk usbc_haintmsk;
1517
1518
1519 usbc_hcint.u32 = cvmx_usb_read_csr32(usb,
1520 CVMX_USBCX_HCINTX(channel, usb->index));
1521
1522 cvmx_usb_write_csr32(usb,
1523 CVMX_USBCX_HCINTX(channel, usb->index),
1524 usbc_hcint.u32);
1525
1526 usbc_hcintmsk.u32 = 0;
1527 usbc_hcintmsk.s.chhltdmsk = 1;
1528 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1529
1530
1531
1532
1533 usbc_hcintmsk.s.datatglerrmsk = 1;
1534 usbc_hcintmsk.s.frmovrunmsk = 1;
1535 usbc_hcintmsk.s.bblerrmsk = 1;
1536 usbc_hcintmsk.s.xacterrmsk = 1;
1537 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1538
1539
1540
1541
1542 usbc_hcintmsk.s.nyetmsk = 1;
1543 usbc_hcintmsk.s.ackmsk = 1;
1544 }
1545 usbc_hcintmsk.s.nakmsk = 1;
1546 usbc_hcintmsk.s.stallmsk = 1;
1547 usbc_hcintmsk.s.xfercomplmsk = 1;
1548 }
1549 cvmx_usb_write_csr32(usb,
1550 CVMX_USBCX_HCINTMSKX(channel, usb->index),
1551 usbc_hcintmsk.u32);
1552
1553
1554 usbc_haintmsk.u32 = cvmx_usb_read_csr32(usb,
1555 CVMX_USBCX_HAINTMSK(usb->index));
1556 usbc_haintmsk.s.haintmsk |= 1 << channel;
1557 cvmx_usb_write_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index),
1558 usbc_haintmsk.u32);
1559 }
1560
1561
1562 {
1563 u64 reg;
1564 u64 dma_address = transaction->buffer +
1565 transaction->actual_bytes;
1566
1567 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1568 dma_address = transaction->buffer +
1569 transaction->iso_packets[0].offset +
1570 transaction->actual_bytes;
1571
1572 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT)
1573 reg = CVMX_USBNX_DMA0_OUTB_CHN0(usb->index);
1574 else
1575 reg = CVMX_USBNX_DMA0_INB_CHN0(usb->index);
1576 cvmx_write64_uint64(reg + channel * 8, dma_address);
1577 }
1578
1579
1580 {
1581 union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
1582 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
1583 int packets_to_transfer;
1584 int bytes_to_transfer = transaction->buffer_length -
1585 transaction->actual_bytes;
1586
1587
1588
1589
1590
1591 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1592 bytes_to_transfer =
1593 transaction->iso_packets[0].length -
1594 transaction->actual_bytes;
1595
1596
1597
1598
1599
1600 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1601
1602
1603
1604
1605
1606
1607 if ((transaction->stage & 1) == 0) {
1608 if (transaction->type == CVMX_USB_TRANSFER_BULK)
1609 pipe->split_sc_frame =
1610 (usb->frame_number + 1) & 0x7f;
1611 else
1612 pipe->split_sc_frame =
1613 (usb->frame_number + 2) & 0x7f;
1614 } else {
1615 pipe->split_sc_frame = -1;
1616 }
1617
1618 usbc_hcsplt.s.spltena = 1;
1619 usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1620 usbc_hcsplt.s.prtaddr = pipe->hub_port;
1621 usbc_hcsplt.s.compsplt = (transaction->stage ==
1622 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
1623
1624
1625
1626
1627
1628
1629 if (bytes_to_transfer > pipe->max_packet)
1630 bytes_to_transfer = pipe->max_packet;
1631
1632
1633
1634
1635
1636
1637 if (!usbc_hcsplt.s.compsplt &&
1638 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1639 (pipe->transfer_type ==
1640 CVMX_USB_TRANSFER_ISOCHRONOUS)) {
1641
1642
1643
1644
1645 pipe->split_sc_frame = -1;
1646
1647
1648
1649
1650 if (transaction->actual_bytes == 0) {
1651
1652
1653
1654
1655 if (bytes_to_transfer <= 188)
1656
1657 usbc_hcsplt.s.xactpos = 3;
1658 else
1659
1660 usbc_hcsplt.s.xactpos = 2;
1661 } else {
1662
1663
1664
1665
1666 if (bytes_to_transfer <= 188)
1667
1668 usbc_hcsplt.s.xactpos = 1;
1669 else
1670
1671 usbc_hcsplt.s.xactpos = 0;
1672 }
1673
1674
1675
1676
1677 if (bytes_to_transfer > 188)
1678 bytes_to_transfer = 188;
1679 }
1680 }
1681
1682
1683
1684
1685
1686
1687 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1688
1689
1690
1691
1692 bytes_to_transfer = MAX_TRANSFER_BYTES /
1693 pipe->max_packet;
1694 bytes_to_transfer *= pipe->max_packet;
1695 }
1696
1697
1698
1699
1700
1701 packets_to_transfer =
1702 DIV_ROUND_UP(bytes_to_transfer, pipe->max_packet);
1703 if (packets_to_transfer == 0) {
1704 packets_to_transfer = 1;
1705 } else if ((packets_to_transfer > 1) &&
1706 (usb->init_flags &
1707 CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1708
1709
1710
1711
1712
1713
1714 packets_to_transfer = 1;
1715 bytes_to_transfer = packets_to_transfer *
1716 pipe->max_packet;
1717 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1718
1719
1720
1721
1722 packets_to_transfer = MAX_TRANSFER_PACKETS;
1723 bytes_to_transfer = packets_to_transfer *
1724 pipe->max_packet;
1725 }
1726
1727 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1728 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1729
1730
1731 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1732
1733
1734
1735 if (pipe->flags & CVMX_USB_PIPE_FLAGS_NEED_PING)
1736 usbc_hctsiz.s.dopng = 1;
1737
1738 cvmx_usb_write_csr32(usb,
1739 CVMX_USBCX_HCSPLTX(channel, usb->index),
1740 usbc_hcsplt.u32);
1741 cvmx_usb_write_csr32(usb,
1742 CVMX_USBCX_HCTSIZX(channel, usb->index),
1743 usbc_hctsiz.u32);
1744 }
1745
1746
1747 {
1748 union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
1749
1750
1751
1752
1753
1754 usbc_hcchar.s.oddfrm = usb->frame_number & 1;
1755
1756
1757
1758
1759
1760
1761
1762 if (cvmx_usb_pipe_needs_split(usb, pipe))
1763 usbc_hcchar.s.ec = 1;
1764 else if (pipe->multi_count < 1)
1765 usbc_hcchar.s.ec = 1;
1766 else if (pipe->multi_count > 3)
1767 usbc_hcchar.s.ec = 3;
1768 else
1769 usbc_hcchar.s.ec = pipe->multi_count;
1770
1771
1772 usbc_hcchar.s.devaddr = pipe->device_addr;
1773 usbc_hcchar.s.eptype = transaction->type;
1774 usbc_hcchar.s.lspddev =
1775 (pipe->device_speed == CVMX_USB_SPEED_LOW);
1776 usbc_hcchar.s.epdir = pipe->transfer_dir;
1777 usbc_hcchar.s.epnum = pipe->endpoint_num;
1778 usbc_hcchar.s.mps = pipe->max_packet;
1779 cvmx_usb_write_csr32(usb,
1780 CVMX_USBCX_HCCHARX(channel, usb->index),
1781 usbc_hcchar.u32);
1782 }
1783
1784
1785 switch (transaction->type) {
1786 case CVMX_USB_TRANSFER_CONTROL:
1787 cvmx_usb_start_channel_control(usb, channel, pipe);
1788 break;
1789 case CVMX_USB_TRANSFER_BULK:
1790 case CVMX_USB_TRANSFER_INTERRUPT:
1791 break;
1792 case CVMX_USB_TRANSFER_ISOCHRONOUS:
1793 if (!cvmx_usb_pipe_needs_split(usb, pipe)) {
1794
1795
1796
1797
1798 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
1799 if (pipe->multi_count < 2)
1800 USB_SET_FIELD32(
1801 CVMX_USBCX_HCTSIZX(channel,
1802 usb->index),
1803 cvmx_usbcx_hctsizx, pid, 0);
1804 else
1805 USB_SET_FIELD32(
1806 CVMX_USBCX_HCTSIZX(channel,
1807 usb->index),
1808 cvmx_usbcx_hctsizx, pid, 3);
1809 }
1810 }
1811 break;
1812 }
1813 {
1814 union cvmx_usbcx_hctsizx usbc_hctsiz = { .u32 =
1815 cvmx_usb_read_csr32(usb,
1816 CVMX_USBCX_HCTSIZX(channel,
1817 usb->index))
1818 };
1819 transaction->xfersize = usbc_hctsiz.s.xfersize;
1820 transaction->pktcnt = usbc_hctsiz.s.pktcnt;
1821 }
1822
1823 if (cvmx_usb_pipe_needs_split(usb, pipe))
1824 usb->active_split = transaction;
1825 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1826 cvmx_usbcx_hccharx, chena, 1);
1827 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1828 cvmx_usb_fill_tx_fifo(usb, channel);
1829}
1830
1831
1832
1833
1834
1835
1836
1837
1838static struct cvmx_usb_pipe *cvmx_usb_find_ready_pipe(struct octeon_hcd *usb,
1839 enum cvmx_usb_transfer xfer_type)
1840{
1841 struct list_head *list = usb->active_pipes + xfer_type;
1842 u64 current_frame = usb->frame_number;
1843 struct cvmx_usb_pipe *pipe;
1844
1845 list_for_each_entry(pipe, list, node) {
1846 struct cvmx_usb_transaction *t =
1847 list_first_entry(&pipe->transactions, typeof(*t),
1848 node);
1849 if (!(pipe->flags & CVMX_USB_PIPE_FLAGS_SCHEDULED) && t &&
1850 (pipe->next_tx_frame <= current_frame) &&
1851 ((pipe->split_sc_frame == -1) ||
1852 ((((int)current_frame - pipe->split_sc_frame) & 0x7f) <
1853 0x40)) &&
1854 (!usb->active_split || (usb->active_split == t))) {
1855 prefetch(t);
1856 return pipe;
1857 }
1858 }
1859 return NULL;
1860}
1861
1862static struct cvmx_usb_pipe *cvmx_usb_next_pipe(struct octeon_hcd *usb,
1863 int is_sof)
1864{
1865 struct cvmx_usb_pipe *pipe;
1866
1867
1868 if (is_sof) {
1869
1870
1871
1872
1873
1874 pipe = cvmx_usb_find_ready_pipe(usb,
1875 CVMX_USB_TRANSFER_ISOCHRONOUS);
1876 if (pipe)
1877 return pipe;
1878 pipe = cvmx_usb_find_ready_pipe(usb,
1879 CVMX_USB_TRANSFER_INTERRUPT);
1880 if (pipe)
1881 return pipe;
1882 }
1883 pipe = cvmx_usb_find_ready_pipe(usb, CVMX_USB_TRANSFER_CONTROL);
1884 if (pipe)
1885 return pipe;
1886 return cvmx_usb_find_ready_pipe(usb, CVMX_USB_TRANSFER_BULK);
1887}
1888
1889
1890
1891
1892
1893
1894
1895
1896static void cvmx_usb_schedule(struct octeon_hcd *usb, int is_sof)
1897{
1898 int channel;
1899 struct cvmx_usb_pipe *pipe;
1900 int need_sof;
1901 enum cvmx_usb_transfer ttype;
1902
1903 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1904
1905
1906
1907
1908 union cvmx_usbcx_hfnum hfnum = {
1909 .u32 = cvmx_usb_read_csr32(usb,
1910 CVMX_USBCX_HFNUM(usb->index))
1911 };
1912
1913 union cvmx_usbcx_hfir hfir = {
1914 .u32 = cvmx_usb_read_csr32(usb,
1915 CVMX_USBCX_HFIR(usb->index))
1916 };
1917
1918 if (hfnum.s.frrem < hfir.s.frint / 4)
1919 goto done;
1920 }
1921
1922 while (usb->idle_hardware_channels) {
1923
1924 channel = __fls(usb->idle_hardware_channels);
1925 if (unlikely(channel > 7))
1926 break;
1927
1928 pipe = cvmx_usb_next_pipe(usb, is_sof);
1929 if (!pipe)
1930 break;
1931
1932 cvmx_usb_start_channel(usb, channel, pipe);
1933 }
1934
1935done:
1936
1937
1938
1939
1940 need_sof = 0;
1941 for (ttype = CVMX_USB_TRANSFER_CONTROL;
1942 ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
1943 list_for_each_entry(pipe, &usb->active_pipes[ttype], node) {
1944 if (pipe->next_tx_frame > usb->frame_number) {
1945 need_sof = 1;
1946 break;
1947 }
1948 }
1949 }
1950 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1951 cvmx_usbcx_gintmsk, sofmsk, need_sof);
1952}
1953
1954static void octeon_usb_urb_complete_callback(struct octeon_hcd *usb,
1955 enum cvmx_usb_status status,
1956 struct cvmx_usb_pipe *pipe,
1957 struct cvmx_usb_transaction
1958 *transaction,
1959 int bytes_transferred,
1960 struct urb *urb)
1961{
1962 struct usb_hcd *hcd = octeon_to_hcd(usb);
1963 struct device *dev = hcd->self.controller;
1964
1965 if (likely(status == CVMX_USB_STATUS_OK))
1966 urb->actual_length = bytes_transferred;
1967 else
1968 urb->actual_length = 0;
1969
1970 urb->hcpriv = NULL;
1971
1972
1973
1974
1975 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1976 int i;
1977
1978
1979
1980
1981 struct cvmx_usb_iso_packet *iso_packet =
1982 (struct cvmx_usb_iso_packet *)urb->setup_packet;
1983
1984 urb->actual_length = 0;
1985 for (i = 0; i < urb->number_of_packets; i++) {
1986 if (iso_packet[i].status == CVMX_USB_STATUS_OK) {
1987 urb->iso_frame_desc[i].status = 0;
1988 urb->iso_frame_desc[i].actual_length =
1989 iso_packet[i].length;
1990 urb->actual_length +=
1991 urb->iso_frame_desc[i].actual_length;
1992 } else {
1993 dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%p transaction=%p size=%d\n",
1994 i, urb->number_of_packets,
1995 iso_packet[i].status, pipe,
1996 transaction, iso_packet[i].length);
1997 urb->iso_frame_desc[i].status = -EREMOTEIO;
1998 }
1999 }
2000
2001 kfree(iso_packet);
2002 urb->setup_packet = NULL;
2003 }
2004
2005 switch (status) {
2006 case CVMX_USB_STATUS_OK:
2007 urb->status = 0;
2008 break;
2009 case CVMX_USB_STATUS_CANCEL:
2010 if (urb->status == 0)
2011 urb->status = -ENOENT;
2012 break;
2013 case CVMX_USB_STATUS_STALL:
2014 dev_dbg(dev, "status=stall pipe=%p transaction=%p size=%d\n",
2015 pipe, transaction, bytes_transferred);
2016 urb->status = -EPIPE;
2017 break;
2018 case CVMX_USB_STATUS_BABBLEERR:
2019 dev_dbg(dev, "status=babble pipe=%p transaction=%p size=%d\n",
2020 pipe, transaction, bytes_transferred);
2021 urb->status = -EPIPE;
2022 break;
2023 case CVMX_USB_STATUS_SHORT:
2024 dev_dbg(dev, "status=short pipe=%p transaction=%p size=%d\n",
2025 pipe, transaction, bytes_transferred);
2026 urb->status = -EREMOTEIO;
2027 break;
2028 case CVMX_USB_STATUS_ERROR:
2029 case CVMX_USB_STATUS_XACTERR:
2030 case CVMX_USB_STATUS_DATATGLERR:
2031 case CVMX_USB_STATUS_FRAMEERR:
2032 dev_dbg(dev, "status=%d pipe=%p transaction=%p size=%d\n",
2033 status, pipe, transaction, bytes_transferred);
2034 urb->status = -EPROTO;
2035 break;
2036 }
2037 usb_hcd_unlink_urb_from_ep(octeon_to_hcd(usb), urb);
2038 spin_unlock(&usb->lock);
2039 usb_hcd_giveback_urb(octeon_to_hcd(usb), urb, urb->status);
2040 spin_lock(&usb->lock);
2041}
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054static void cvmx_usb_complete(struct octeon_hcd *usb,
2055 struct cvmx_usb_pipe *pipe,
2056 struct cvmx_usb_transaction *transaction,
2057 enum cvmx_usb_status complete_code)
2058{
2059
2060 if (usb->active_split == transaction)
2061 usb->active_split = NULL;
2062
2063
2064
2065
2066
2067 if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
2068
2069 transaction->iso_packets[0].length = transaction->actual_bytes;
2070 transaction->iso_packets[0].status = complete_code;
2071
2072
2073
2074
2075
2076 if ((transaction->iso_number_packets > 1) &&
2077 (complete_code == CVMX_USB_STATUS_OK)) {
2078
2079 transaction->actual_bytes = 0;
2080
2081 transaction->iso_number_packets--;
2082
2083 transaction->iso_packets++;
2084 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2085 return;
2086 }
2087 }
2088
2089
2090 list_del(&transaction->node);
2091 if (list_empty(&pipe->transactions))
2092 list_move_tail(&pipe->node, &usb->idle_pipes);
2093 octeon_usb_urb_complete_callback(usb, complete_code, pipe,
2094 transaction,
2095 transaction->actual_bytes,
2096 transaction->urb);
2097 kfree(transaction);
2098}
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122static struct cvmx_usb_transaction *cvmx_usb_submit_transaction(
2123 struct octeon_hcd *usb,
2124 struct cvmx_usb_pipe *pipe,
2125 enum cvmx_usb_transfer type,
2126 u64 buffer,
2127 int buffer_length,
2128 u64 control_header,
2129 int iso_start_frame,
2130 int iso_number_packets,
2131 struct cvmx_usb_iso_packet *iso_packets,
2132 struct urb *urb)
2133{
2134 struct cvmx_usb_transaction *transaction;
2135
2136 if (unlikely(pipe->transfer_type != type))
2137 return NULL;
2138
2139 transaction = kzalloc(sizeof(*transaction), GFP_ATOMIC);
2140 if (unlikely(!transaction))
2141 return NULL;
2142
2143 transaction->type = type;
2144 transaction->buffer = buffer;
2145 transaction->buffer_length = buffer_length;
2146 transaction->control_header = control_header;
2147
2148 transaction->iso_start_frame = iso_start_frame;
2149 transaction->iso_number_packets = iso_number_packets;
2150 transaction->iso_packets = iso_packets;
2151 transaction->urb = urb;
2152 if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2153 transaction->stage = CVMX_USB_STAGE_SETUP;
2154 else
2155 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2156
2157 if (!list_empty(&pipe->transactions)) {
2158 list_add_tail(&transaction->node, &pipe->transactions);
2159 } else {
2160 list_add_tail(&transaction->node, &pipe->transactions);
2161 list_move_tail(&pipe->node,
2162 &usb->active_pipes[pipe->transfer_type]);
2163
2164
2165
2166
2167
2168 cvmx_usb_schedule(usb, 0);
2169 }
2170
2171 return transaction;
2172}
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183static struct cvmx_usb_transaction *cvmx_usb_submit_bulk(
2184 struct octeon_hcd *usb,
2185 struct cvmx_usb_pipe *pipe,
2186 struct urb *urb)
2187{
2188 return cvmx_usb_submit_transaction(usb, pipe, CVMX_USB_TRANSFER_BULK,
2189 urb->transfer_dma,
2190 urb->transfer_buffer_length,
2191 0,
2192 0,
2193 0,
2194 NULL,
2195 urb);
2196}
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207static struct cvmx_usb_transaction *cvmx_usb_submit_interrupt(
2208 struct octeon_hcd *usb,
2209 struct cvmx_usb_pipe *pipe,
2210 struct urb *urb)
2211{
2212 return cvmx_usb_submit_transaction(usb, pipe,
2213 CVMX_USB_TRANSFER_INTERRUPT,
2214 urb->transfer_dma,
2215 urb->transfer_buffer_length,
2216 0,
2217 0,
2218 0,
2219 NULL,
2220 urb);
2221}
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232static struct cvmx_usb_transaction *cvmx_usb_submit_control(
2233 struct octeon_hcd *usb,
2234 struct cvmx_usb_pipe *pipe,
2235 struct urb *urb)
2236{
2237 int buffer_length = urb->transfer_buffer_length;
2238 u64 control_header = urb->setup_dma;
2239 struct usb_ctrlrequest *header = cvmx_phys_to_ptr(control_header);
2240
2241 if ((header->bRequestType & USB_DIR_IN) == 0)
2242 buffer_length = le16_to_cpu(header->wLength);
2243
2244 return cvmx_usb_submit_transaction(usb, pipe,
2245 CVMX_USB_TRANSFER_CONTROL,
2246 urb->transfer_dma, buffer_length,
2247 control_header,
2248 0,
2249 0,
2250 NULL,
2251 urb);
2252}
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263static struct cvmx_usb_transaction *cvmx_usb_submit_isochronous(
2264 struct octeon_hcd *usb,
2265 struct cvmx_usb_pipe *pipe,
2266 struct urb *urb)
2267{
2268 struct cvmx_usb_iso_packet *packets;
2269
2270 packets = (struct cvmx_usb_iso_packet *)urb->setup_packet;
2271 return cvmx_usb_submit_transaction(usb, pipe,
2272 CVMX_USB_TRANSFER_ISOCHRONOUS,
2273 urb->transfer_dma,
2274 urb->transfer_buffer_length,
2275 0,
2276 urb->start_frame,
2277 urb->number_of_packets,
2278 packets, urb);
2279}
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294static int cvmx_usb_cancel(struct octeon_hcd *usb,
2295 struct cvmx_usb_pipe *pipe,
2296 struct cvmx_usb_transaction *transaction)
2297{
2298
2299
2300
2301
2302 if (list_first_entry(&pipe->transactions, typeof(*transaction), node) ==
2303 transaction && (pipe->flags & CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
2304 union cvmx_usbcx_hccharx usbc_hcchar;
2305
2306 usb->pipe_for_channel[pipe->channel] = NULL;
2307 pipe->flags &= ~CVMX_USB_PIPE_FLAGS_SCHEDULED;
2308
2309 CVMX_SYNCW;
2310
2311 usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2312 CVMX_USBCX_HCCHARX(pipe->channel, usb->index));
2313
2314
2315
2316
2317 if (usbc_hcchar.s.chena) {
2318 usbc_hcchar.s.chdis = 1;
2319 cvmx_usb_write_csr32(usb,
2320 CVMX_USBCX_HCCHARX(pipe->channel,
2321 usb->index),
2322 usbc_hcchar.u32);
2323 }
2324 }
2325 cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_CANCEL);
2326 return 0;
2327}
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338static int cvmx_usb_cancel_all(struct octeon_hcd *usb,
2339 struct cvmx_usb_pipe *pipe)
2340{
2341 struct cvmx_usb_transaction *transaction, *next;
2342
2343
2344 list_for_each_entry_safe(transaction, next, &pipe->transactions, node) {
2345 int result = cvmx_usb_cancel(usb, pipe, transaction);
2346
2347 if (unlikely(result != 0))
2348 return result;
2349 }
2350 return 0;
2351}
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362static int cvmx_usb_close_pipe(struct octeon_hcd *usb,
2363 struct cvmx_usb_pipe *pipe)
2364{
2365
2366 if (!list_empty(&pipe->transactions))
2367 return -EBUSY;
2368
2369 list_del(&pipe->node);
2370 kfree(pipe);
2371
2372 return 0;
2373}
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383static int cvmx_usb_get_frame_number(struct octeon_hcd *usb)
2384{
2385 union cvmx_usbcx_hfnum usbc_hfnum;
2386
2387 usbc_hfnum.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2388
2389 return usbc_hfnum.s.frnum;
2390}
2391
2392static void cvmx_usb_transfer_control(struct octeon_hcd *usb,
2393 struct cvmx_usb_pipe *pipe,
2394 struct cvmx_usb_transaction *transaction,
2395 union cvmx_usbcx_hccharx usbc_hcchar,
2396 int buffer_space_left,
2397 int bytes_in_last_packet)
2398{
2399 switch (transaction->stage) {
2400 case CVMX_USB_STAGE_NON_CONTROL:
2401 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
2402
2403 cvmx_usb_complete(usb, pipe, transaction,
2404 CVMX_USB_STATUS_ERROR);
2405 break;
2406 case CVMX_USB_STAGE_SETUP:
2407 pipe->pid_toggle = 1;
2408 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2409 transaction->stage =
2410 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
2411 } else {
2412 struct usb_ctrlrequest *header =
2413 cvmx_phys_to_ptr(transaction->control_header);
2414 if (header->wLength)
2415 transaction->stage = CVMX_USB_STAGE_DATA;
2416 else
2417 transaction->stage = CVMX_USB_STAGE_STATUS;
2418 }
2419 break;
2420 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
2421 {
2422 struct usb_ctrlrequest *header =
2423 cvmx_phys_to_ptr(transaction->control_header);
2424 if (header->wLength)
2425 transaction->stage = CVMX_USB_STAGE_DATA;
2426 else
2427 transaction->stage = CVMX_USB_STAGE_STATUS;
2428 }
2429 break;
2430 case CVMX_USB_STAGE_DATA:
2431 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2432 transaction->stage = CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
2433
2434
2435
2436
2437
2438
2439 if (!usbc_hcchar.s.epdir) {
2440 if (buffer_space_left < pipe->max_packet)
2441 transaction->actual_bytes +=
2442 buffer_space_left;
2443 else
2444 transaction->actual_bytes +=
2445 pipe->max_packet;
2446 }
2447 } else if ((buffer_space_left == 0) ||
2448 (bytes_in_last_packet < pipe->max_packet)) {
2449 pipe->pid_toggle = 1;
2450 transaction->stage = CVMX_USB_STAGE_STATUS;
2451 }
2452 break;
2453 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
2454 if ((buffer_space_left == 0) ||
2455 (bytes_in_last_packet < pipe->max_packet)) {
2456 pipe->pid_toggle = 1;
2457 transaction->stage = CVMX_USB_STAGE_STATUS;
2458 } else {
2459 transaction->stage = CVMX_USB_STAGE_DATA;
2460 }
2461 break;
2462 case CVMX_USB_STAGE_STATUS:
2463 if (cvmx_usb_pipe_needs_split(usb, pipe))
2464 transaction->stage =
2465 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
2466 else
2467 cvmx_usb_complete(usb, pipe, transaction,
2468 CVMX_USB_STATUS_OK);
2469 break;
2470 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
2471 cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2472 break;
2473 }
2474}
2475
2476static void cvmx_usb_transfer_bulk(struct octeon_hcd *usb,
2477 struct cvmx_usb_pipe *pipe,
2478 struct cvmx_usb_transaction *transaction,
2479 union cvmx_usbcx_hcintx usbc_hcint,
2480 int buffer_space_left,
2481 int bytes_in_last_packet)
2482{
2483
2484
2485
2486
2487
2488 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2489 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL)
2490 transaction->stage =
2491 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2492 else if (buffer_space_left &&
2493 (bytes_in_last_packet == pipe->max_packet))
2494 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2495 else
2496 cvmx_usb_complete(usb, pipe, transaction,
2497 CVMX_USB_STATUS_OK);
2498 } else {
2499 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2500 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
2501 (usbc_hcint.s.nak))
2502 pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
2503 if (!buffer_space_left ||
2504 (bytes_in_last_packet < pipe->max_packet))
2505 cvmx_usb_complete(usb, pipe, transaction,
2506 CVMX_USB_STATUS_OK);
2507 }
2508}
2509
2510static void cvmx_usb_transfer_intr(struct octeon_hcd *usb,
2511 struct cvmx_usb_pipe *pipe,
2512 struct cvmx_usb_transaction *transaction,
2513 int buffer_space_left,
2514 int bytes_in_last_packet)
2515{
2516 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2517 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL) {
2518 transaction->stage =
2519 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2520 } else if (buffer_space_left &&
2521 (bytes_in_last_packet == pipe->max_packet)) {
2522 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2523 } else {
2524 pipe->next_tx_frame += pipe->interval;
2525 cvmx_usb_complete(usb, pipe, transaction,
2526 CVMX_USB_STATUS_OK);
2527 }
2528 } else if (!buffer_space_left ||
2529 (bytes_in_last_packet < pipe->max_packet)) {
2530 pipe->next_tx_frame += pipe->interval;
2531 cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2532 }
2533}
2534
2535static void cvmx_usb_transfer_isoc(struct octeon_hcd *usb,
2536 struct cvmx_usb_pipe *pipe,
2537 struct cvmx_usb_transaction *transaction,
2538 int buffer_space_left,
2539 int bytes_in_last_packet,
2540 int bytes_this_transfer)
2541{
2542 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2543
2544
2545
2546
2547
2548
2549 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
2550
2551
2552
2553
2554
2555 if (!buffer_space_left || (bytes_this_transfer < 188)) {
2556 pipe->next_tx_frame += pipe->interval;
2557 cvmx_usb_complete(usb, pipe, transaction,
2558 CVMX_USB_STATUS_OK);
2559 }
2560 return;
2561 }
2562 if (transaction->stage ==
2563 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
2564
2565
2566
2567
2568 if ((buffer_space_left == 0) ||
2569 (bytes_in_last_packet < pipe->max_packet)) {
2570 pipe->next_tx_frame += pipe->interval;
2571 cvmx_usb_complete(usb, pipe, transaction,
2572 CVMX_USB_STATUS_OK);
2573 }
2574 } else {
2575 transaction->stage =
2576 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2577 }
2578 } else {
2579 pipe->next_tx_frame += pipe->interval;
2580 cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2581 }
2582}
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592static int cvmx_usb_poll_channel(struct octeon_hcd *usb, int channel)
2593{
2594 struct usb_hcd *hcd = octeon_to_hcd(usb);
2595 struct device *dev = hcd->self.controller;
2596 union cvmx_usbcx_hcintx usbc_hcint;
2597 union cvmx_usbcx_hctsizx usbc_hctsiz;
2598 union cvmx_usbcx_hccharx usbc_hcchar;
2599 struct cvmx_usb_pipe *pipe;
2600 struct cvmx_usb_transaction *transaction;
2601 int bytes_this_transfer;
2602 int bytes_in_last_packet;
2603 int packets_processed;
2604 int buffer_space_left;
2605
2606
2607 usbc_hcint.u32 = cvmx_usb_read_csr32(usb,
2608 CVMX_USBCX_HCINTX(channel, usb->index));
2609
2610 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2611 usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2612 CVMX_USBCX_HCCHARX(channel, usb->index));
2613
2614 if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
2615
2616
2617
2618
2619
2620 cvmx_usb_write_csr32(usb,
2621 CVMX_USBCX_HCCHARX(channel,
2622 usb->index),
2623 usbc_hcchar.u32);
2624 return 0;
2625 }
2626
2627
2628
2629
2630
2631 if (!usbc_hcint.s.chhltd) {
2632 if (usbc_hcchar.s.chena) {
2633 union cvmx_usbcx_hcintmskx hcintmsk;
2634
2635 hcintmsk.u32 = 0;
2636 hcintmsk.s.chhltdmsk = 1;
2637 cvmx_usb_write_csr32(usb,
2638 CVMX_USBCX_HCINTMSKX(channel, usb->index),
2639 hcintmsk.u32);
2640 usbc_hcchar.s.chdis = 1;
2641 cvmx_usb_write_csr32(usb,
2642 CVMX_USBCX_HCCHARX(channel, usb->index),
2643 usbc_hcchar.u32);
2644 return 0;
2645 } else if (usbc_hcint.s.xfercompl) {
2646
2647
2648
2649
2650 } else {
2651 dev_err(dev, "USB%d: Channel %d interrupt without halt\n",
2652 usb->index, channel);
2653 return 0;
2654 }
2655 }
2656 } else {
2657
2658
2659
2660
2661 if (!usbc_hcint.s.chhltd)
2662 return 0;
2663 }
2664
2665
2666 cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
2667 usb->idle_hardware_channels |= (1 << channel);
2668
2669
2670 pipe = usb->pipe_for_channel[channel];
2671 prefetch(pipe);
2672 if (!pipe)
2673 return 0;
2674 transaction = list_first_entry(&pipe->transactions,
2675 typeof(*transaction),
2676 node);
2677 prefetch(transaction);
2678
2679
2680
2681
2682
2683 usb->pipe_for_channel[channel] = NULL;
2684 pipe->flags &= ~CVMX_USB_PIPE_FLAGS_SCHEDULED;
2685
2686
2687
2688
2689
2690 usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2691 CVMX_USBCX_HCCHARX(channel, usb->index));
2692 usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
2693 CVMX_USBCX_HCTSIZX(channel, usb->index));
2694
2695
2696
2697
2698
2699 packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2700 if (usbc_hcchar.s.epdir) {
2701
2702
2703
2704
2705
2706
2707 bytes_this_transfer = transaction->xfersize -
2708 usbc_hctsiz.s.xfersize;
2709 } else {
2710
2711
2712
2713
2714
2715
2716 bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
2717
2718
2719
2720
2721 if (bytes_this_transfer > transaction->xfersize)
2722 bytes_this_transfer = transaction->xfersize;
2723 }
2724
2725 if (packets_processed)
2726 bytes_in_last_packet = bytes_this_transfer -
2727 (packets_processed - 1) * usbc_hcchar.s.mps;
2728 else
2729 bytes_in_last_packet = bytes_this_transfer;
2730
2731
2732
2733
2734
2735
2736 if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
2737 (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
2738 bytes_this_transfer = 0;
2739
2740
2741
2742
2743
2744
2745 transaction->actual_bytes += bytes_this_transfer;
2746 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
2747 buffer_space_left = transaction->iso_packets[0].length -
2748 transaction->actual_bytes;
2749 else
2750 buffer_space_left = transaction->buffer_length -
2751 transaction->actual_bytes;
2752
2753
2754
2755
2756
2757 pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
2758
2759
2760
2761
2762
2763
2764 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2765 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2766 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
2767 pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
2768
2769 if (WARN_ON_ONCE(bytes_this_transfer < 0)) {
2770
2771
2772
2773
2774
2775 cvmx_usb_complete(usb, pipe, transaction,
2776 CVMX_USB_STATUS_ERROR);
2777 return 0;
2778 }
2779
2780 if (usbc_hcint.s.stall) {
2781
2782
2783
2784
2785
2786
2787 pipe->pid_toggle = 0;
2788 cvmx_usb_complete(usb, pipe, transaction,
2789 CVMX_USB_STATUS_STALL);
2790 } else if (usbc_hcint.s.xacterr) {
2791
2792
2793
2794
2795
2796 cvmx_usb_complete(usb, pipe, transaction,
2797 CVMX_USB_STATUS_XACTERR);
2798 } else if (usbc_hcint.s.bblerr) {
2799
2800 cvmx_usb_complete(usb, pipe, transaction,
2801 CVMX_USB_STATUS_BABBLEERR);
2802 } else if (usbc_hcint.s.datatglerr) {
2803
2804 cvmx_usb_complete(usb, pipe, transaction,
2805 CVMX_USB_STATUS_DATATGLERR);
2806 } else if (usbc_hcint.s.nyet) {
2807
2808
2809
2810
2811
2812
2813 if (!cvmx_usb_pipe_needs_split(usb, pipe)) {
2814 transaction->retries = 0;
2815
2816
2817
2818
2819 if ((buffer_space_left == 0) ||
2820 (bytes_in_last_packet < pipe->max_packet))
2821 cvmx_usb_complete(usb, pipe,
2822 transaction,
2823 CVMX_USB_STATUS_OK);
2824 } else {
2825
2826
2827
2828
2829
2830 transaction->retries++;
2831 if ((transaction->retries & 0x3) == 0) {
2832
2833
2834
2835
2836 transaction->stage &= ~1;
2837 pipe->split_sc_frame = -1;
2838 }
2839 }
2840 } else if (usbc_hcint.s.ack) {
2841 transaction->retries = 0;
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853 pipe->flags &= ~CVMX_USB_PIPE_FLAGS_NEED_PING;
2854
2855 switch (transaction->type) {
2856 case CVMX_USB_TRANSFER_CONTROL:
2857 cvmx_usb_transfer_control(usb, pipe, transaction,
2858 usbc_hcchar,
2859 buffer_space_left,
2860 bytes_in_last_packet);
2861 break;
2862 case CVMX_USB_TRANSFER_BULK:
2863 cvmx_usb_transfer_bulk(usb, pipe, transaction,
2864 usbc_hcint, buffer_space_left,
2865 bytes_in_last_packet);
2866 break;
2867 case CVMX_USB_TRANSFER_INTERRUPT:
2868 cvmx_usb_transfer_intr(usb, pipe, transaction,
2869 buffer_space_left,
2870 bytes_in_last_packet);
2871 break;
2872 case CVMX_USB_TRANSFER_ISOCHRONOUS:
2873 cvmx_usb_transfer_isoc(usb, pipe, transaction,
2874 buffer_space_left,
2875 bytes_in_last_packet,
2876 bytes_this_transfer);
2877 break;
2878 }
2879 } else if (usbc_hcint.s.nak) {
2880
2881
2882
2883 if (usb->active_split == transaction)
2884 usb->active_split = NULL;
2885
2886
2887
2888
2889
2890
2891 transaction->retries = 0;
2892 transaction->stage &= ~1;
2893 pipe->next_tx_frame += pipe->interval;
2894 if (pipe->next_tx_frame < usb->frame_number)
2895 pipe->next_tx_frame = usb->frame_number +
2896 pipe->interval -
2897 (usb->frame_number - pipe->next_tx_frame) %
2898 pipe->interval;
2899 } else {
2900 struct cvmx_usb_port_status port;
2901
2902 port = cvmx_usb_get_status(usb);
2903 if (port.port_enabled) {
2904
2905 transaction->retries++;
2906 } else {
2907
2908
2909
2910
2911 cvmx_usb_complete(usb, pipe, transaction,
2912 CVMX_USB_STATUS_ERROR);
2913 }
2914 }
2915 return 0;
2916}
2917
2918static void octeon_usb_port_callback(struct octeon_hcd *usb)
2919{
2920 spin_unlock(&usb->lock);
2921 usb_hcd_poll_rh_status(octeon_to_hcd(usb));
2922 spin_lock(&usb->lock);
2923}
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935static int cvmx_usb_poll(struct octeon_hcd *usb)
2936{
2937 union cvmx_usbcx_hfnum usbc_hfnum;
2938 union cvmx_usbcx_gintsts usbc_gintsts;
2939
2940 prefetch_range(usb, sizeof(*usb));
2941
2942
2943 usbc_hfnum.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2944 if ((usb->frame_number & 0x3fff) > usbc_hfnum.s.frnum)
2945 usb->frame_number += 0x4000;
2946 usb->frame_number &= ~0x3fffull;
2947 usb->frame_number |= usbc_hfnum.s.frnum;
2948
2949
2950 usbc_gintsts.u32 = cvmx_usb_read_csr32(usb,
2951 CVMX_USBCX_GINTSTS(usb->index));
2952
2953
2954 cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index),
2955 usbc_gintsts.u32);
2956
2957 if (usbc_gintsts.s.rxflvl) {
2958
2959
2960
2961
2962
2963
2964
2965 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
2966 cvmx_usb_poll_rx_fifo(usb);
2967 }
2968 if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
2969
2970 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
2971 cvmx_usb_poll_tx_fifo(usb);
2972 }
2973 if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
2974 union cvmx_usbcx_hprt usbc_hprt;
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989 octeon_usb_port_callback(usb);
2990
2991 usbc_hprt.u32 =
2992 cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
2993 usbc_hprt.s.prtena = 0;
2994 cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index),
2995 usbc_hprt.u32);
2996 }
2997 if (usbc_gintsts.s.hchint) {
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010 union cvmx_usbcx_haint usbc_haint;
3011
3012 usbc_haint.u32 = cvmx_usb_read_csr32(usb,
3013 CVMX_USBCX_HAINT(usb->index));
3014 while (usbc_haint.u32) {
3015 int channel;
3016
3017 channel = __fls(usbc_haint.u32);
3018 cvmx_usb_poll_channel(usb, channel);
3019 usbc_haint.u32 ^= 1 << channel;
3020 }
3021 }
3022
3023 cvmx_usb_schedule(usb, usbc_gintsts.s.sof);
3024
3025 return 0;
3026}
3027
3028
3029static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
3030{
3031 return (struct octeon_hcd *)(hcd->hcd_priv);
3032}
3033
3034static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
3035{
3036 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3037 unsigned long flags;
3038
3039 spin_lock_irqsave(&usb->lock, flags);
3040 cvmx_usb_poll(usb);
3041 spin_unlock_irqrestore(&usb->lock, flags);
3042 return IRQ_HANDLED;
3043}
3044
3045static int octeon_usb_start(struct usb_hcd *hcd)
3046{
3047 hcd->state = HC_STATE_RUNNING;
3048 return 0;
3049}
3050
3051static void octeon_usb_stop(struct usb_hcd *hcd)
3052{
3053 hcd->state = HC_STATE_HALT;
3054}
3055
3056static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
3057{
3058 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3059
3060 return cvmx_usb_get_frame_number(usb);
3061}
3062
3063static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
3064 struct urb *urb,
3065 gfp_t mem_flags)
3066{
3067 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3068 struct device *dev = hcd->self.controller;
3069 struct cvmx_usb_transaction *transaction = NULL;
3070 struct cvmx_usb_pipe *pipe;
3071 unsigned long flags;
3072 struct cvmx_usb_iso_packet *iso_packet;
3073 struct usb_host_endpoint *ep = urb->ep;
3074 int rc;
3075
3076 urb->status = 0;
3077 spin_lock_irqsave(&usb->lock, flags);
3078
3079 rc = usb_hcd_link_urb_to_ep(hcd, urb);
3080 if (rc) {
3081 spin_unlock_irqrestore(&usb->lock, flags);
3082 return rc;
3083 }
3084
3085 if (!ep->hcpriv) {
3086 enum cvmx_usb_transfer transfer_type;
3087 enum cvmx_usb_speed speed;
3088 int split_device = 0;
3089 int split_port = 0;
3090
3091 switch (usb_pipetype(urb->pipe)) {
3092 case PIPE_ISOCHRONOUS:
3093 transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
3094 break;
3095 case PIPE_INTERRUPT:
3096 transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
3097 break;
3098 case PIPE_CONTROL:
3099 transfer_type = CVMX_USB_TRANSFER_CONTROL;
3100 break;
3101 default:
3102 transfer_type = CVMX_USB_TRANSFER_BULK;
3103 break;
3104 }
3105 switch (urb->dev->speed) {
3106 case USB_SPEED_LOW:
3107 speed = CVMX_USB_SPEED_LOW;
3108 break;
3109 case USB_SPEED_FULL:
3110 speed = CVMX_USB_SPEED_FULL;
3111 break;
3112 default:
3113 speed = CVMX_USB_SPEED_HIGH;
3114 break;
3115 }
3116
3117
3118
3119
3120
3121 if (speed != CVMX_USB_SPEED_HIGH) {
3122
3123
3124
3125
3126 struct usb_device *dev = urb->dev;
3127
3128 while (dev->parent) {
3129
3130
3131
3132
3133 if (dev->parent->speed == USB_SPEED_HIGH) {
3134 split_device = dev->parent->devnum;
3135 split_port = dev->portnum;
3136 break;
3137 }
3138
3139
3140
3141
3142
3143
3144 dev = dev->parent;
3145 }
3146 }
3147 pipe = cvmx_usb_open_pipe(usb, usb_pipedevice(urb->pipe),
3148 usb_pipeendpoint(urb->pipe), speed,
3149 le16_to_cpu(ep->desc.wMaxPacketSize)
3150 & 0x7ff,
3151 transfer_type,
3152 usb_pipein(urb->pipe) ?
3153 CVMX_USB_DIRECTION_IN :
3154 CVMX_USB_DIRECTION_OUT,
3155 urb->interval,
3156 (le16_to_cpu(ep->desc.wMaxPacketSize)
3157 >> 11) & 0x3,
3158 split_device, split_port);
3159 if (!pipe) {
3160 usb_hcd_unlink_urb_from_ep(hcd, urb);
3161 spin_unlock_irqrestore(&usb->lock, flags);
3162 dev_dbg(dev, "Failed to create pipe\n");
3163 return -ENOMEM;
3164 }
3165 ep->hcpriv = pipe;
3166 } else {
3167 pipe = ep->hcpriv;
3168 }
3169
3170 switch (usb_pipetype(urb->pipe)) {
3171 case PIPE_ISOCHRONOUS:
3172 dev_dbg(dev, "Submit isochronous to %d.%d\n",
3173 usb_pipedevice(urb->pipe),
3174 usb_pipeendpoint(urb->pipe));
3175
3176
3177
3178
3179 iso_packet = kmalloc_array(urb->number_of_packets,
3180 sizeof(struct cvmx_usb_iso_packet),
3181 GFP_ATOMIC);
3182 if (iso_packet) {
3183 int i;
3184
3185 for (i = 0; i < urb->number_of_packets; i++) {
3186 iso_packet[i].offset =
3187 urb->iso_frame_desc[i].offset;
3188 iso_packet[i].length =
3189 urb->iso_frame_desc[i].length;
3190 iso_packet[i].status = CVMX_USB_STATUS_ERROR;
3191 }
3192
3193
3194
3195
3196
3197 urb->setup_packet = (char *)iso_packet;
3198 transaction = cvmx_usb_submit_isochronous(usb,
3199 pipe, urb);
3200
3201
3202
3203
3204 if (!transaction) {
3205 urb->setup_packet = NULL;
3206 kfree(iso_packet);
3207 }
3208 }
3209 break;
3210 case PIPE_INTERRUPT:
3211 dev_dbg(dev, "Submit interrupt to %d.%d\n",
3212 usb_pipedevice(urb->pipe),
3213 usb_pipeendpoint(urb->pipe));
3214 transaction = cvmx_usb_submit_interrupt(usb, pipe, urb);
3215 break;
3216 case PIPE_CONTROL:
3217 dev_dbg(dev, "Submit control to %d.%d\n",
3218 usb_pipedevice(urb->pipe),
3219 usb_pipeendpoint(urb->pipe));
3220 transaction = cvmx_usb_submit_control(usb, pipe, urb);
3221 break;
3222 case PIPE_BULK:
3223 dev_dbg(dev, "Submit bulk to %d.%d\n",
3224 usb_pipedevice(urb->pipe),
3225 usb_pipeendpoint(urb->pipe));
3226 transaction = cvmx_usb_submit_bulk(usb, pipe, urb);
3227 break;
3228 }
3229 if (!transaction) {
3230 usb_hcd_unlink_urb_from_ep(hcd, urb);
3231 spin_unlock_irqrestore(&usb->lock, flags);
3232 dev_dbg(dev, "Failed to submit\n");
3233 return -ENOMEM;
3234 }
3235 urb->hcpriv = transaction;
3236 spin_unlock_irqrestore(&usb->lock, flags);
3237 return 0;
3238}
3239
3240static int octeon_usb_urb_dequeue(struct usb_hcd *hcd,
3241 struct urb *urb,
3242 int status)
3243{
3244 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3245 unsigned long flags;
3246 int rc;
3247
3248 if (!urb->dev)
3249 return -EINVAL;
3250
3251 spin_lock_irqsave(&usb->lock, flags);
3252
3253 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
3254 if (rc)
3255 goto out;
3256
3257 urb->status = status;
3258 cvmx_usb_cancel(usb, urb->ep->hcpriv, urb->hcpriv);
3259
3260out:
3261 spin_unlock_irqrestore(&usb->lock, flags);
3262
3263 return rc;
3264}
3265
3266static void octeon_usb_endpoint_disable(struct usb_hcd *hcd,
3267 struct usb_host_endpoint *ep)
3268{
3269 struct device *dev = hcd->self.controller;
3270
3271 if (ep->hcpriv) {
3272 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3273 struct cvmx_usb_pipe *pipe = ep->hcpriv;
3274 unsigned long flags;
3275
3276 spin_lock_irqsave(&usb->lock, flags);
3277 cvmx_usb_cancel_all(usb, pipe);
3278 if (cvmx_usb_close_pipe(usb, pipe))
3279 dev_dbg(dev, "Closing pipe %p failed\n", pipe);
3280 spin_unlock_irqrestore(&usb->lock, flags);
3281 ep->hcpriv = NULL;
3282 }
3283}
3284
3285static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
3286{
3287 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3288 struct cvmx_usb_port_status port_status;
3289 unsigned long flags;
3290
3291 spin_lock_irqsave(&usb->lock, flags);
3292 port_status = cvmx_usb_get_status(usb);
3293 spin_unlock_irqrestore(&usb->lock, flags);
3294 buf[0] = port_status.connect_change << 1;
3295
3296 return buf[0] != 0;
3297}
3298
3299static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3300 u16 wIndex, char *buf, u16 wLength)
3301{
3302 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3303 struct device *dev = hcd->self.controller;
3304 struct cvmx_usb_port_status usb_port_status;
3305 int port_status;
3306 struct usb_hub_descriptor *desc;
3307 unsigned long flags;
3308
3309 switch (typeReq) {
3310 case ClearHubFeature:
3311 dev_dbg(dev, "ClearHubFeature\n");
3312 switch (wValue) {
3313 case C_HUB_LOCAL_POWER:
3314 case C_HUB_OVER_CURRENT:
3315
3316 break;
3317 default:
3318 return -EINVAL;
3319 }
3320 break;
3321 case ClearPortFeature:
3322 dev_dbg(dev, "ClearPortFeature\n");
3323 if (wIndex != 1) {
3324 dev_dbg(dev, " INVALID\n");
3325 return -EINVAL;
3326 }
3327
3328 switch (wValue) {
3329 case USB_PORT_FEAT_ENABLE:
3330 dev_dbg(dev, " ENABLE\n");
3331 spin_lock_irqsave(&usb->lock, flags);
3332 cvmx_usb_disable(usb);
3333 spin_unlock_irqrestore(&usb->lock, flags);
3334 break;
3335 case USB_PORT_FEAT_SUSPEND:
3336 dev_dbg(dev, " SUSPEND\n");
3337
3338 break;
3339 case USB_PORT_FEAT_POWER:
3340 dev_dbg(dev, " POWER\n");
3341
3342 break;
3343 case USB_PORT_FEAT_INDICATOR:
3344 dev_dbg(dev, " INDICATOR\n");
3345
3346 break;
3347 case USB_PORT_FEAT_C_CONNECTION:
3348 dev_dbg(dev, " C_CONNECTION\n");
3349
3350 spin_lock_irqsave(&usb->lock, flags);
3351 usb->port_status = cvmx_usb_get_status(usb);
3352 spin_unlock_irqrestore(&usb->lock, flags);
3353 break;
3354 case USB_PORT_FEAT_C_RESET:
3355 dev_dbg(dev, " C_RESET\n");
3356
3357
3358
3359 spin_lock_irqsave(&usb->lock, flags);
3360 usb->port_status = cvmx_usb_get_status(usb);
3361 spin_unlock_irqrestore(&usb->lock, flags);
3362 break;
3363 case USB_PORT_FEAT_C_ENABLE:
3364 dev_dbg(dev, " C_ENABLE\n");
3365
3366
3367
3368
3369 spin_lock_irqsave(&usb->lock, flags);
3370 usb->port_status = cvmx_usb_get_status(usb);
3371 spin_unlock_irqrestore(&usb->lock, flags);
3372 break;
3373 case USB_PORT_FEAT_C_SUSPEND:
3374 dev_dbg(dev, " C_SUSPEND\n");
3375
3376
3377
3378
3379
3380 break;
3381 case USB_PORT_FEAT_C_OVER_CURRENT:
3382 dev_dbg(dev, " C_OVER_CURRENT\n");
3383
3384 spin_lock_irqsave(&usb->lock, flags);
3385 usb->port_status = cvmx_usb_get_status(usb);
3386 spin_unlock_irqrestore(&usb->lock, flags);
3387 break;
3388 default:
3389 dev_dbg(dev, " UNKNOWN\n");
3390 return -EINVAL;
3391 }
3392 break;
3393 case GetHubDescriptor:
3394 dev_dbg(dev, "GetHubDescriptor\n");
3395 desc = (struct usb_hub_descriptor *)buf;
3396 desc->bDescLength = 9;
3397 desc->bDescriptorType = 0x29;
3398 desc->bNbrPorts = 1;
3399 desc->wHubCharacteristics = cpu_to_le16(0x08);
3400 desc->bPwrOn2PwrGood = 1;
3401 desc->bHubContrCurrent = 0;
3402 desc->u.hs.DeviceRemovable[0] = 0;
3403 desc->u.hs.DeviceRemovable[1] = 0xff;
3404 break;
3405 case GetHubStatus:
3406 dev_dbg(dev, "GetHubStatus\n");
3407 *(__le32 *)buf = 0;
3408 break;
3409 case GetPortStatus:
3410 dev_dbg(dev, "GetPortStatus\n");
3411 if (wIndex != 1) {
3412 dev_dbg(dev, " INVALID\n");
3413 return -EINVAL;
3414 }
3415
3416 spin_lock_irqsave(&usb->lock, flags);
3417 usb_port_status = cvmx_usb_get_status(usb);
3418 spin_unlock_irqrestore(&usb->lock, flags);
3419 port_status = 0;
3420
3421 if (usb_port_status.connect_change) {
3422 port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
3423 dev_dbg(dev, " C_CONNECTION\n");
3424 }
3425
3426 if (usb_port_status.port_enabled) {
3427 port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
3428 dev_dbg(dev, " C_ENABLE\n");
3429 }
3430
3431 if (usb_port_status.connected) {
3432 port_status |= (1 << USB_PORT_FEAT_CONNECTION);
3433 dev_dbg(dev, " CONNECTION\n");
3434 }
3435
3436 if (usb_port_status.port_enabled) {
3437 port_status |= (1 << USB_PORT_FEAT_ENABLE);
3438 dev_dbg(dev, " ENABLE\n");
3439 }
3440
3441 if (usb_port_status.port_over_current) {
3442 port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
3443 dev_dbg(dev, " OVER_CURRENT\n");
3444 }
3445
3446 if (usb_port_status.port_powered) {
3447 port_status |= (1 << USB_PORT_FEAT_POWER);
3448 dev_dbg(dev, " POWER\n");
3449 }
3450
3451 if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
3452 port_status |= USB_PORT_STAT_HIGH_SPEED;
3453 dev_dbg(dev, " HIGHSPEED\n");
3454 } else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
3455 port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
3456 dev_dbg(dev, " LOWSPEED\n");
3457 }
3458
3459 *((__le32 *)buf) = cpu_to_le32(port_status);
3460 break;
3461 case SetHubFeature:
3462 dev_dbg(dev, "SetHubFeature\n");
3463
3464 break;
3465 case SetPortFeature:
3466 dev_dbg(dev, "SetPortFeature\n");
3467 if (wIndex != 1) {
3468 dev_dbg(dev, " INVALID\n");
3469 return -EINVAL;
3470 }
3471
3472 switch (wValue) {
3473 case USB_PORT_FEAT_SUSPEND:
3474 dev_dbg(dev, " SUSPEND\n");
3475 return -EINVAL;
3476 case USB_PORT_FEAT_POWER:
3477 dev_dbg(dev, " POWER\n");
3478
3479
3480
3481 spin_lock_irqsave(&usb->lock, flags);
3482 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index),
3483 cvmx_usbcx_hprt, prtpwr, 1);
3484 spin_unlock_irqrestore(&usb->lock, flags);
3485 return 0;
3486 case USB_PORT_FEAT_RESET:
3487 dev_dbg(dev, " RESET\n");
3488 spin_lock_irqsave(&usb->lock, flags);
3489 cvmx_usb_reset_port(usb);
3490 spin_unlock_irqrestore(&usb->lock, flags);
3491 return 0;
3492 case USB_PORT_FEAT_INDICATOR:
3493 dev_dbg(dev, " INDICATOR\n");
3494
3495 break;
3496 default:
3497 dev_dbg(dev, " UNKNOWN\n");
3498 return -EINVAL;
3499 }
3500 break;
3501 default:
3502 dev_dbg(dev, "Unknown root hub request\n");
3503 return -EINVAL;
3504 }
3505 return 0;
3506}
3507
3508static const struct hc_driver octeon_hc_driver = {
3509 .description = "Octeon USB",
3510 .product_desc = "Octeon Host Controller",
3511 .hcd_priv_size = sizeof(struct octeon_hcd),
3512 .irq = octeon_usb_irq,
3513 .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
3514 .start = octeon_usb_start,
3515 .stop = octeon_usb_stop,
3516 .urb_enqueue = octeon_usb_urb_enqueue,
3517 .urb_dequeue = octeon_usb_urb_dequeue,
3518 .endpoint_disable = octeon_usb_endpoint_disable,
3519 .get_frame_number = octeon_usb_get_frame_number,
3520 .hub_status_data = octeon_usb_hub_status_data,
3521 .hub_control = octeon_usb_hub_control,
3522 .map_urb_for_dma = octeon_map_urb_for_dma,
3523 .unmap_urb_for_dma = octeon_unmap_urb_for_dma,
3524};
3525
3526static int octeon_usb_probe(struct platform_device *pdev)
3527{
3528 int status;
3529 int initialize_flags;
3530 int usb_num;
3531 struct resource *res_mem;
3532 struct device_node *usbn_node;
3533 int irq = platform_get_irq(pdev, 0);
3534 struct device *dev = &pdev->dev;
3535 struct octeon_hcd *usb;
3536 struct usb_hcd *hcd;
3537 u32 clock_rate = 48000000;
3538 bool is_crystal_clock = false;
3539 const char *clock_type;
3540 int i;
3541
3542 if (!dev->of_node) {
3543 dev_err(dev, "Error: empty of_node\n");
3544 return -ENXIO;
3545 }
3546 usbn_node = dev->of_node->parent;
3547
3548 i = of_property_read_u32(usbn_node,
3549 "clock-frequency", &clock_rate);
3550 if (i)
3551 i = of_property_read_u32(usbn_node,
3552 "refclk-frequency", &clock_rate);
3553 if (i) {
3554 dev_err(dev, "No USBN \"clock-frequency\"\n");
3555 return -ENXIO;
3556 }
3557 switch (clock_rate) {
3558 case 12000000:
3559 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
3560 break;
3561 case 24000000:
3562 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
3563 break;
3564 case 48000000:
3565 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
3566 break;
3567 default:
3568 dev_err(dev, "Illegal USBN \"clock-frequency\" %u\n",
3569 clock_rate);
3570 return -ENXIO;
3571 }
3572
3573 i = of_property_read_string(usbn_node,
3574 "cavium,refclk-type", &clock_type);
3575 if (i)
3576 i = of_property_read_string(usbn_node,
3577 "refclk-type", &clock_type);
3578
3579 if (!i && strcmp("crystal", clock_type) == 0)
3580 is_crystal_clock = true;
3581
3582 if (is_crystal_clock)
3583 initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;
3584 else
3585 initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
3586
3587 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3588 if (!res_mem) {
3589 dev_err(dev, "found no memory resource\n");
3590 return -ENXIO;
3591 }
3592 usb_num = (res_mem->start >> 44) & 1;
3593
3594 if (irq < 0) {
3595
3596 irq_hw_number_t hwirq = usb_num ? (1 << 6) + 17 : 56;
3597
3598 irq = irq_create_mapping(NULL, hwirq);
3599 }
3600
3601
3602
3603
3604
3605 i = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
3606 if (i)
3607 return i;
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620 if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
3621 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
3622
3623 pri_cnt.u64 = 0;
3624 pri_cnt.s.cnt_enb = 1;
3625 pri_cnt.s.cnt_val = 400;
3626 cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
3627 }
3628
3629 hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
3630 if (!hcd) {
3631 dev_dbg(dev, "Failed to allocate memory for HCD\n");
3632 return -1;
3633 }
3634 hcd->uses_new_polling = 1;
3635 usb = (struct octeon_hcd *)hcd->hcd_priv;
3636
3637 spin_lock_init(&usb->lock);
3638
3639 usb->init_flags = initialize_flags;
3640
3641
3642 usb->index = usb_num;
3643 INIT_LIST_HEAD(&usb->idle_pipes);
3644 for (i = 0; i < ARRAY_SIZE(usb->active_pipes); i++)
3645 INIT_LIST_HEAD(&usb->active_pipes[i]);
3646
3647
3648 if (OCTEON_IS_MODEL(OCTEON_CN31XX)) {
3649 usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
3650
3651 usb->idle_hardware_channels = 0x1;
3652 } else if (OCTEON_IS_MODEL(OCTEON_CN5XXX)) {
3653
3654 usb->idle_hardware_channels = 0xf7;
3655 } else {
3656 usb->idle_hardware_channels = 0xff;
3657 }
3658
3659 status = cvmx_usb_initialize(dev, usb);
3660 if (status) {
3661 dev_dbg(dev, "USB initialization failed with %d\n", status);
3662 usb_put_hcd(hcd);
3663 return -1;
3664 }
3665
3666 status = usb_add_hcd(hcd, irq, 0);
3667 if (status) {
3668 dev_dbg(dev, "USB add HCD failed with %d\n", status);
3669 usb_put_hcd(hcd);
3670 return -1;
3671 }
3672 device_wakeup_enable(hcd->self.controller);
3673
3674 dev_info(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
3675
3676 return 0;
3677}
3678
3679static int octeon_usb_remove(struct platform_device *pdev)
3680{
3681 int status;
3682 struct device *dev = &pdev->dev;
3683 struct usb_hcd *hcd = dev_get_drvdata(dev);
3684 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3685 unsigned long flags;
3686
3687 usb_remove_hcd(hcd);
3688 spin_lock_irqsave(&usb->lock, flags);
3689 status = cvmx_usb_shutdown(usb);
3690 spin_unlock_irqrestore(&usb->lock, flags);
3691 if (status)
3692 dev_dbg(dev, "USB shutdown failed with %d\n", status);
3693
3694 usb_put_hcd(hcd);
3695
3696 return 0;
3697}
3698
3699static const struct of_device_id octeon_usb_match[] = {
3700 {
3701 .compatible = "cavium,octeon-5750-usbc",
3702 },
3703 {},
3704};
3705MODULE_DEVICE_TABLE(of, octeon_usb_match);
3706
3707static struct platform_driver octeon_usb_driver = {
3708 .driver = {
3709 .name = "octeon-hcd",
3710 .of_match_table = octeon_usb_match,
3711 },
3712 .probe = octeon_usb_probe,
3713 .remove = octeon_usb_remove,
3714};
3715
3716static int __init octeon_usb_driver_init(void)
3717{
3718 if (usb_disabled())
3719 return 0;
3720
3721 return platform_driver_register(&octeon_usb_driver);
3722}
3723module_init(octeon_usb_driver_init);
3724
3725static void __exit octeon_usb_driver_exit(void)
3726{
3727 if (usb_disabled())
3728 return;
3729
3730 platform_driver_unregister(&octeon_usb_driver);
3731}
3732module_exit(octeon_usb_driver_exit);
3733
3734MODULE_LICENSE("GPL");
3735MODULE_AUTHOR("Cavium, Inc. <support@cavium.com>");
3736MODULE_DESCRIPTION("Cavium Inc. OCTEON USB Host driver.");
3737