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