0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #include <linux/usb.h>
0049 #include <linux/slab.h>
0050 #include <linux/module.h>
0051 #include <linux/usb/hcd.h>
0052 #include <linux/prefetch.h>
0053 #include <linux/irqdomain.h>
0054 #include <linux/dma-mapping.h>
0055 #include <linux/platform_device.h>
0056 #include <linux/of.h>
0057
0058 #include <asm/octeon/octeon.h>
0059
0060 #include "octeon-hcd.h"
0061
0062
0063
0064
0065
0066
0067
0068
0069 enum cvmx_usb_speed {
0070 CVMX_USB_SPEED_HIGH = 0,
0071 CVMX_USB_SPEED_FULL = 1,
0072 CVMX_USB_SPEED_LOW = 2,
0073 };
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 enum cvmx_usb_transfer {
0088 CVMX_USB_TRANSFER_CONTROL = 0,
0089 CVMX_USB_TRANSFER_ISOCHRONOUS = 1,
0090 CVMX_USB_TRANSFER_BULK = 2,
0091 CVMX_USB_TRANSFER_INTERRUPT = 3,
0092 };
0093
0094
0095
0096
0097
0098
0099
0100 enum cvmx_usb_direction {
0101 CVMX_USB_DIRECTION_OUT,
0102 CVMX_USB_DIRECTION_IN,
0103 };
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 enum cvmx_usb_status {
0126 CVMX_USB_STATUS_OK,
0127 CVMX_USB_STATUS_SHORT,
0128 CVMX_USB_STATUS_CANCEL,
0129 CVMX_USB_STATUS_ERROR,
0130 CVMX_USB_STATUS_STALL,
0131 CVMX_USB_STATUS_XACTERR,
0132 CVMX_USB_STATUS_DATATGLERR,
0133 CVMX_USB_STATUS_BABBLEERR,
0134 CVMX_USB_STATUS_FRAMEERR,
0135 };
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 struct cvmx_usb_port_status {
0153 u32 reserved : 25;
0154 u32 port_enabled : 1;
0155 u32 port_over_current : 1;
0156 u32 port_powered : 1;
0157 enum cvmx_usb_speed port_speed : 2;
0158 u32 connected : 1;
0159 u32 connect_change : 1;
0160 };
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170 struct cvmx_usb_iso_packet {
0171 int offset;
0172 int length;
0173 enum cvmx_usb_status status;
0174 };
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193 enum cvmx_usb_initialize_flags {
0194 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI = 1 << 0,
0195 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND = 1 << 1,
0196 CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK = 3 << 3,
0197 CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ = 1 << 3,
0198 CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ = 2 << 3,
0199 CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ = 3 << 3,
0200
0201 CVMX_USB_INITIALIZE_FLAGS_NO_DMA = 1 << 5,
0202 };
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 enum cvmx_usb_pipe_flags {
0213 CVMX_USB_PIPE_FLAGS_SCHEDULED = 1 << 17,
0214 CVMX_USB_PIPE_FLAGS_NEED_PING = 1 << 18,
0215 };
0216
0217
0218 #define MAX_RETRIES 3
0219
0220
0221 #define MAX_CHANNELS 8
0222
0223
0224
0225
0226
0227 #define MAX_TRANSFER_BYTES ((1 << 19) - 1)
0228
0229
0230
0231
0232
0233 #define MAX_TRANSFER_PACKETS ((1 << 10) - 1)
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 enum cvmx_usb_stage {
0244 CVMX_USB_STAGE_NON_CONTROL,
0245 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE,
0246 CVMX_USB_STAGE_SETUP,
0247 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE,
0248 CVMX_USB_STAGE_DATA,
0249 CVMX_USB_STAGE_DATA_SPLIT_COMPLETE,
0250 CVMX_USB_STAGE_STATUS,
0251 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE,
0252 };
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274 struct cvmx_usb_transaction {
0275 struct list_head node;
0276 enum cvmx_usb_transfer type;
0277 u64 buffer;
0278 int buffer_length;
0279 u64 control_header;
0280 int iso_start_frame;
0281 int iso_number_packets;
0282 struct cvmx_usb_iso_packet *iso_packets;
0283 int xfersize;
0284 int pktcnt;
0285 int retries;
0286 int actual_bytes;
0287 enum cvmx_usb_stage stage;
0288 struct urb *urb;
0289 };
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318 struct cvmx_usb_pipe {
0319 struct list_head node;
0320 struct list_head transactions;
0321 u64 interval;
0322 u64 next_tx_frame;
0323 enum cvmx_usb_pipe_flags flags;
0324 enum cvmx_usb_speed device_speed;
0325 enum cvmx_usb_transfer transfer_type;
0326 enum cvmx_usb_direction transfer_dir;
0327 int multi_count;
0328 u16 max_packet;
0329 u8 device_addr;
0330 u8 endpoint_num;
0331 u8 hub_device_addr;
0332 u8 hub_port;
0333 u8 pid_toggle;
0334 u8 channel;
0335 s8 split_sc_frame;
0336 };
0337
0338 struct cvmx_usb_tx_fifo {
0339 struct {
0340 int channel;
0341 int size;
0342 u64 address;
0343 } entry[MAX_CHANNELS + 1];
0344 int head;
0345 int tail;
0346 };
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366 struct octeon_hcd {
0367 spinlock_t lock;
0368 int init_flags;
0369 int index;
0370 int idle_hardware_channels;
0371 union cvmx_usbcx_hprt usbcx_hprt;
0372 struct cvmx_usb_pipe *pipe_for_channel[MAX_CHANNELS];
0373 int indent;
0374 struct cvmx_usb_port_status port_status;
0375 struct list_head idle_pipes;
0376 struct list_head active_pipes[4];
0377 u64 frame_number;
0378 struct cvmx_usb_transaction *active_split;
0379 struct cvmx_usb_tx_fifo periodic;
0380 struct cvmx_usb_tx_fifo nonperiodic;
0381 };
0382
0383
0384
0385
0386
0387 #define USB_SET_FIELD32(address, _union, field, value) \
0388 do { \
0389 union _union c; \
0390 \
0391 c.u32 = cvmx_usb_read_csr32(usb, address); \
0392 c.s.field = value; \
0393 cvmx_usb_write_csr32(usb, address, c.u32); \
0394 } while (0)
0395
0396
0397 #define USB_FIFO_ADDRESS(channel, usb_index) \
0398 (CVMX_USBCX_GOTGCTL(usb_index) + ((channel) + 1) * 0x1000)
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409 struct octeon_temp_buffer {
0410 void *orig_buffer;
0411 u8 data[];
0412 };
0413
0414 static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
0415 {
0416 return container_of((void *)p, struct usb_hcd, hcd_priv);
0417 }
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428 static int octeon_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
0429 {
0430 struct octeon_temp_buffer *temp;
0431
0432 if (urb->num_sgs || urb->sg ||
0433 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) ||
0434 !(urb->transfer_buffer_length % sizeof(u32)))
0435 return 0;
0436
0437 temp = kmalloc(ALIGN(urb->transfer_buffer_length, sizeof(u32)) +
0438 sizeof(*temp), mem_flags);
0439 if (!temp)
0440 return -ENOMEM;
0441
0442 temp->orig_buffer = urb->transfer_buffer;
0443 if (usb_urb_dir_out(urb))
0444 memcpy(temp->data, urb->transfer_buffer,
0445 urb->transfer_buffer_length);
0446 urb->transfer_buffer = temp->data;
0447 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
0448
0449 return 0;
0450 }
0451
0452
0453
0454
0455
0456
0457
0458 static void octeon_free_temp_buffer(struct urb *urb)
0459 {
0460 struct octeon_temp_buffer *temp;
0461 size_t length;
0462
0463 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
0464 return;
0465
0466 temp = container_of(urb->transfer_buffer, struct octeon_temp_buffer,
0467 data);
0468 if (usb_urb_dir_in(urb)) {
0469 if (usb_pipeisoc(urb->pipe))
0470 length = urb->transfer_buffer_length;
0471 else
0472 length = urb->actual_length;
0473
0474 memcpy(temp->orig_buffer, urb->transfer_buffer, length);
0475 }
0476 urb->transfer_buffer = temp->orig_buffer;
0477 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
0478 kfree(temp);
0479 }
0480
0481
0482
0483
0484
0485
0486
0487 static int octeon_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
0488 gfp_t mem_flags)
0489 {
0490 int ret;
0491
0492 ret = octeon_alloc_temp_buffer(urb, mem_flags);
0493 if (ret)
0494 return ret;
0495
0496 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
0497 if (ret)
0498 octeon_free_temp_buffer(urb);
0499
0500 return ret;
0501 }
0502
0503
0504
0505
0506
0507
0508 static void octeon_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
0509 {
0510 usb_hcd_unmap_urb_for_dma(hcd, urb);
0511 octeon_free_temp_buffer(urb);
0512 }
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524 static inline u32 cvmx_usb_read_csr32(struct octeon_hcd *usb, u64 address)
0525 {
0526 return cvmx_read64_uint32(address ^ 4);
0527 }
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538 static inline void cvmx_usb_write_csr32(struct octeon_hcd *usb,
0539 u64 address, u32 value)
0540 {
0541 cvmx_write64_uint32(address ^ 4, value);
0542 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
0543 }
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554 static inline int cvmx_usb_pipe_needs_split(struct octeon_hcd *usb,
0555 struct cvmx_usb_pipe *pipe)
0556 {
0557 return pipe->device_speed != CVMX_USB_SPEED_HIGH &&
0558 usb->usbcx_hprt.s.prtspd == CVMX_USB_SPEED_HIGH;
0559 }
0560
0561
0562
0563
0564
0565
0566
0567
0568 static inline int cvmx_usb_get_data_pid(struct cvmx_usb_pipe *pipe)
0569 {
0570 if (pipe->pid_toggle)
0571 return 2;
0572 return 0;
0573 }
0574
0575
0576 static int cvmx_wait_tx_rx(struct octeon_hcd *usb, int fflsh_type)
0577 {
0578 int result;
0579 u64 address = CVMX_USBCX_GRSTCTL(usb->index);
0580 u64 done = cvmx_get_cycle() + 100 *
0581 (u64)octeon_get_clock_rate / 1000000;
0582 union cvmx_usbcx_grstctl c;
0583
0584 while (1) {
0585 c.u32 = cvmx_usb_read_csr32(usb, address);
0586 if (fflsh_type == 0 && c.s.txfflsh == 0) {
0587 result = 0;
0588 break;
0589 } else if (fflsh_type == 1 && c.s.rxfflsh == 0) {
0590 result = 0;
0591 break;
0592 } else if (cvmx_get_cycle() > done) {
0593 result = -1;
0594 break;
0595 }
0596
0597 __delay(100);
0598 }
0599 return result;
0600 }
0601
0602 static void cvmx_fifo_setup(struct octeon_hcd *usb)
0603 {
0604 union cvmx_usbcx_ghwcfg3 usbcx_ghwcfg3;
0605 union cvmx_usbcx_gnptxfsiz npsiz;
0606 union cvmx_usbcx_hptxfsiz psiz;
0607
0608 usbcx_ghwcfg3.u32 = cvmx_usb_read_csr32(usb,
0609 CVMX_USBCX_GHWCFG3(usb->index));
0610
0611
0612
0613
0614
0615 USB_SET_FIELD32(CVMX_USBCX_GRXFSIZ(usb->index), cvmx_usbcx_grxfsiz,
0616 rxfdep, usbcx_ghwcfg3.s.dfifodepth / 4);
0617
0618
0619
0620
0621
0622
0623 npsiz.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index));
0624 npsiz.s.nptxfdep = usbcx_ghwcfg3.s.dfifodepth / 2;
0625 npsiz.s.nptxfstaddr = usbcx_ghwcfg3.s.dfifodepth / 4;
0626 cvmx_usb_write_csr32(usb, CVMX_USBCX_GNPTXFSIZ(usb->index), npsiz.u32);
0627
0628
0629
0630
0631
0632
0633 psiz.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index));
0634 psiz.s.ptxfsize = usbcx_ghwcfg3.s.dfifodepth / 4;
0635 psiz.s.ptxfstaddr = 3 * usbcx_ghwcfg3.s.dfifodepth / 4;
0636 cvmx_usb_write_csr32(usb, CVMX_USBCX_HPTXFSIZ(usb->index), psiz.u32);
0637
0638
0639 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
0640 cvmx_usbcx_grstctl, txfnum, 0x10);
0641 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
0642 cvmx_usbcx_grstctl, txfflsh, 1);
0643 cvmx_wait_tx_rx(usb, 0);
0644 USB_SET_FIELD32(CVMX_USBCX_GRSTCTL(usb->index),
0645 cvmx_usbcx_grstctl, rxfflsh, 1);
0646 cvmx_wait_tx_rx(usb, 1);
0647 }
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658 static int cvmx_usb_shutdown(struct octeon_hcd *usb)
0659 {
0660 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
0661
0662
0663 if (!list_empty(&usb->idle_pipes) ||
0664 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_ISOCHRONOUS]) ||
0665 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_INTERRUPT]) ||
0666 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_CONTROL]) ||
0667 !list_empty(&usb->active_pipes[CVMX_USB_TRANSFER_BULK]))
0668 return -EBUSY;
0669
0670
0671 usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
0672 usbn_clk_ctl.s.enable = 1;
0673 usbn_clk_ctl.s.por = 1;
0674 usbn_clk_ctl.s.hclk_rst = 1;
0675 usbn_clk_ctl.s.prst = 0;
0676 usbn_clk_ctl.s.hrst = 0;
0677 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
0678 return 0;
0679 }
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691 static int cvmx_usb_initialize(struct device *dev,
0692 struct octeon_hcd *usb)
0693 {
0694 int channel;
0695 int divisor;
0696 int retries = 0;
0697 union cvmx_usbcx_hcfg usbcx_hcfg;
0698 union cvmx_usbnx_clk_ctl usbn_clk_ctl;
0699 union cvmx_usbcx_gintsts usbc_gintsts;
0700 union cvmx_usbcx_gahbcfg usbcx_gahbcfg;
0701 union cvmx_usbcx_gintmsk usbcx_gintmsk;
0702 union cvmx_usbcx_gusbcfg usbcx_gusbcfg;
0703 union cvmx_usbnx_usbp_ctl_status usbn_usbp_ctl_status;
0704
0705 retry:
0706
0707
0708
0709
0710
0711
0712
0713
0714 usbn_clk_ctl.u64 = cvmx_read64_uint64(CVMX_USBNX_CLK_CTL(usb->index));
0715 usbn_clk_ctl.s.por = 1;
0716 usbn_clk_ctl.s.hrst = 0;
0717 usbn_clk_ctl.s.prst = 0;
0718 usbn_clk_ctl.s.hclk_rst = 0;
0719 usbn_clk_ctl.s.enable = 0;
0720
0721
0722
0723
0724 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND) {
0725
0726
0727
0728
0729
0730 if (OCTEON_IS_MODEL(OCTEON_CN3XXX) ||
0731 OCTEON_IS_MODEL(OCTEON_CN56XX) ||
0732 OCTEON_IS_MODEL(OCTEON_CN50XX))
0733
0734 usbn_clk_ctl.s.p_rtype = 2;
0735 else
0736
0737 usbn_clk_ctl.s.p_rtype = 1;
0738
0739 switch (usb->init_flags &
0740 CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK) {
0741 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ:
0742 usbn_clk_ctl.s.p_c_sel = 0;
0743 break;
0744 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ:
0745 usbn_clk_ctl.s.p_c_sel = 1;
0746 break;
0747 case CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ:
0748 usbn_clk_ctl.s.p_c_sel = 2;
0749 break;
0750 }
0751 } else {
0752
0753
0754
0755
0756 if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
0757
0758 usbn_clk_ctl.s.p_rtype = 3;
0759 else
0760
0761 usbn_clk_ctl.s.p_rtype = 0;
0762
0763 usbn_clk_ctl.s.p_c_sel = 0;
0764 }
0765
0766
0767
0768
0769
0770 divisor = DIV_ROUND_UP(octeon_get_clock_rate(), 125000000);
0771
0772 if (divisor < 4)
0773 divisor = 4;
0774 usbn_clk_ctl.s.divide = divisor;
0775 usbn_clk_ctl.s.divide2 = 0;
0776 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
0777
0778
0779 usbn_clk_ctl.s.hclk_rst = 1;
0780 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
0781
0782 __delay(64);
0783
0784
0785
0786
0787
0788 usbn_clk_ctl.s.por = 0;
0789 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
0790
0791 mdelay(1);
0792
0793
0794
0795
0796
0797 usbn_usbp_ctl_status.u64 =
0798 cvmx_read64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index));
0799 usbn_usbp_ctl_status.s.ate_reset = 1;
0800 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
0801 usbn_usbp_ctl_status.u64);
0802
0803 __delay(10);
0804
0805
0806
0807
0808 usbn_usbp_ctl_status.s.ate_reset = 0;
0809 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
0810 usbn_usbp_ctl_status.u64);
0811
0812
0813
0814
0815 usbn_clk_ctl.s.prst = 1;
0816 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
0817
0818
0819
0820
0821
0822 usbn_usbp_ctl_status.s.hst_mode = 0;
0823 cvmx_write64_uint64(CVMX_USBNX_USBP_CTL_STATUS(usb->index),
0824 usbn_usbp_ctl_status.u64);
0825
0826 udelay(1);
0827
0828
0829
0830
0831 usbn_clk_ctl.s.hrst = 1;
0832 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
0833
0834 usbn_clk_ctl.s.enable = 1;
0835 cvmx_write64_uint64(CVMX_USBNX_CLK_CTL(usb->index), usbn_clk_ctl.u64);
0836 udelay(1);
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856 usbcx_gahbcfg.u32 = 0;
0857 usbcx_gahbcfg.s.dmaen = !(usb->init_flags &
0858 CVMX_USB_INITIALIZE_FLAGS_NO_DMA);
0859 usbcx_gahbcfg.s.hbstlen = 0;
0860 usbcx_gahbcfg.s.nptxfemplvl = 1;
0861 usbcx_gahbcfg.s.ptxfemplvl = 1;
0862 usbcx_gahbcfg.s.glblintrmsk = 1;
0863 cvmx_usb_write_csr32(usb, CVMX_USBCX_GAHBCFG(usb->index),
0864 usbcx_gahbcfg.u32);
0865
0866
0867
0868
0869
0870
0871
0872
0873 usbcx_gusbcfg.u32 = cvmx_usb_read_csr32(usb,
0874 CVMX_USBCX_GUSBCFG(usb->index));
0875 usbcx_gusbcfg.s.toutcal = 0;
0876 usbcx_gusbcfg.s.ddrsel = 0;
0877 usbcx_gusbcfg.s.usbtrdtim = 0x5;
0878 usbcx_gusbcfg.s.phylpwrclksel = 0;
0879 cvmx_usb_write_csr32(usb, CVMX_USBCX_GUSBCFG(usb->index),
0880 usbcx_gusbcfg.u32);
0881
0882
0883
0884
0885
0886
0887
0888 usbcx_gintmsk.u32 = cvmx_usb_read_csr32(usb,
0889 CVMX_USBCX_GINTMSK(usb->index));
0890 usbcx_gintmsk.s.otgintmsk = 1;
0891 usbcx_gintmsk.s.modemismsk = 1;
0892 usbcx_gintmsk.s.hchintmsk = 1;
0893 usbcx_gintmsk.s.sofmsk = 0;
0894
0895 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
0896 usbcx_gintmsk.s.rxflvlmsk = 1;
0897 cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTMSK(usb->index),
0898 usbcx_gintmsk.u32);
0899
0900
0901
0902
0903 for (channel = 0; channel < 8; channel++)
0904 cvmx_usb_write_csr32(usb,
0905 CVMX_USBCX_HCINTMSKX(channel, usb->index),
0906 0);
0907
0908
0909
0910
0911
0912
0913
0914 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
0915 cvmx_usbcx_gintmsk, prtintmsk, 1);
0916 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
0917 cvmx_usbcx_gintmsk, disconnintmsk, 1);
0918
0919
0920
0921
0922
0923 usbcx_hcfg.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HCFG(usb->index));
0924 usbcx_hcfg.s.fslssupp = 0;
0925 usbcx_hcfg.s.fslspclksel = 0;
0926 cvmx_usb_write_csr32(usb, CVMX_USBCX_HCFG(usb->index), usbcx_hcfg.u32);
0927
0928 cvmx_fifo_setup(usb);
0929
0930
0931
0932
0933
0934
0935 usbc_gintsts.u32 = cvmx_usb_read_csr32(usb,
0936 CVMX_USBCX_GINTSTS(usb->index));
0937 cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index),
0938 usbc_gintsts.u32);
0939 dev_dbg(dev, "gintsts after reset: 0x%x\n", (int)usbc_gintsts.u32);
0940 if (!usbc_gintsts.s.disconnint && !usbc_gintsts.s.prtint)
0941 return 0;
0942 if (retries++ >= 5)
0943 return -EAGAIN;
0944 dev_info(dev, "controller reset failed (gintsts=0x%x) - retrying\n",
0945 (int)usbc_gintsts.u32);
0946 msleep(50);
0947 cvmx_usb_shutdown(usb);
0948 msleep(50);
0949 goto retry;
0950 }
0951
0952
0953
0954
0955
0956
0957
0958 static void cvmx_usb_reset_port(struct octeon_hcd *usb)
0959 {
0960 usb->usbcx_hprt.u32 = cvmx_usb_read_csr32(usb,
0961 CVMX_USBCX_HPRT(usb->index));
0962
0963
0964 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
0965 prtrst, 1);
0966
0967
0968
0969
0970
0971 mdelay(50);
0972
0973
0974 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
0975 prtrst, 0);
0976
0977
0978
0979
0980
0981 usb->usbcx_hprt.u32 = cvmx_usb_read_csr32(usb,
0982 CVMX_USBCX_HPRT(usb->index));
0983 }
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993
0994
0995 static int cvmx_usb_disable(struct octeon_hcd *usb)
0996 {
0997
0998 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index), cvmx_usbcx_hprt,
0999 prtena, 1);
1000 return 0;
1001 }
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014 static struct cvmx_usb_port_status cvmx_usb_get_status(struct octeon_hcd *usb)
1015 {
1016 union cvmx_usbcx_hprt usbc_hprt;
1017 struct cvmx_usb_port_status result;
1018
1019 memset(&result, 0, sizeof(result));
1020
1021 usbc_hprt.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
1022 result.port_enabled = usbc_hprt.s.prtena;
1023 result.port_over_current = usbc_hprt.s.prtovrcurract;
1024 result.port_powered = usbc_hprt.s.prtpwr;
1025 result.port_speed = usbc_hprt.s.prtspd;
1026 result.connected = usbc_hprt.s.prtconnsts;
1027 result.connect_change =
1028 result.connected != usb->port_status.connected;
1029
1030 return result;
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
1083
1084
1085 static struct cvmx_usb_pipe *cvmx_usb_open_pipe(struct octeon_hcd *usb,
1086 int device_addr,
1087 int endpoint_num,
1088 enum cvmx_usb_speed
1089 device_speed,
1090 int max_packet,
1091 enum cvmx_usb_transfer
1092 transfer_type,
1093 enum cvmx_usb_direction
1094 transfer_dir,
1095 int interval, int multi_count,
1096 int hub_device_addr,
1097 int hub_port)
1098 {
1099 struct cvmx_usb_pipe *pipe;
1100
1101 pipe = kzalloc(sizeof(*pipe), GFP_ATOMIC);
1102 if (!pipe)
1103 return NULL;
1104 if ((device_speed == CVMX_USB_SPEED_HIGH) &&
1105 (transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1106 (transfer_type == CVMX_USB_TRANSFER_BULK))
1107 pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
1108 pipe->device_addr = device_addr;
1109 pipe->endpoint_num = endpoint_num;
1110 pipe->device_speed = device_speed;
1111 pipe->max_packet = max_packet;
1112 pipe->transfer_type = transfer_type;
1113 pipe->transfer_dir = transfer_dir;
1114 INIT_LIST_HEAD(&pipe->transactions);
1115
1116
1117
1118
1119
1120 if (!interval)
1121 interval = 1;
1122 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1123 pipe->interval = interval * 8;
1124
1125 pipe->next_tx_frame = ((usb->frame_number + 7) & ~7) +
1126 pipe->interval;
1127 } else {
1128 pipe->interval = interval;
1129 pipe->next_tx_frame = usb->frame_number + pipe->interval;
1130 }
1131 pipe->multi_count = multi_count;
1132 pipe->hub_device_addr = hub_device_addr;
1133 pipe->hub_port = hub_port;
1134 pipe->pid_toggle = 0;
1135 pipe->split_sc_frame = -1;
1136 list_add_tail(&pipe->node, &usb->idle_pipes);
1137
1138
1139
1140
1141
1142
1143 return pipe;
1144 }
1145
1146
1147
1148
1149
1150
1151
1152
1153 static void cvmx_usb_poll_rx_fifo(struct octeon_hcd *usb)
1154 {
1155 union cvmx_usbcx_grxstsph rx_status;
1156 int channel;
1157 int bytes;
1158 u64 address;
1159 u32 *ptr;
1160
1161 rx_status.u32 = cvmx_usb_read_csr32(usb,
1162 CVMX_USBCX_GRXSTSPH(usb->index));
1163
1164 if (rx_status.s.pktsts != 2)
1165 return;
1166
1167 if (!rx_status.s.bcnt)
1168 return;
1169
1170 channel = rx_status.s.chnum;
1171 bytes = rx_status.s.bcnt;
1172 if (!bytes)
1173 return;
1174
1175
1176 address = cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) +
1177 channel * 8);
1178
1179 ptr = cvmx_phys_to_ptr(address);
1180 cvmx_write64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index) + channel * 8,
1181 address + bytes);
1182
1183
1184 while (bytes > 0) {
1185 *ptr++ = cvmx_usb_read_csr32(usb,
1186 USB_FIFO_ADDRESS(channel, usb->index));
1187 bytes -= 4;
1188 }
1189 CVMX_SYNCW;
1190 }
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203 static int cvmx_usb_fill_tx_hw(struct octeon_hcd *usb,
1204 struct cvmx_usb_tx_fifo *fifo, int available)
1205 {
1206
1207
1208
1209
1210 while (available && (fifo->head != fifo->tail)) {
1211 int i = fifo->tail;
1212 const u32 *ptr = cvmx_phys_to_ptr(fifo->entry[i].address);
1213 u64 csr_address = USB_FIFO_ADDRESS(fifo->entry[i].channel,
1214 usb->index) ^ 4;
1215 int words = available;
1216
1217
1218 if (fifo->entry[i].size <= available) {
1219 words = fifo->entry[i].size;
1220 fifo->tail++;
1221 if (fifo->tail > MAX_CHANNELS)
1222 fifo->tail = 0;
1223 }
1224
1225
1226 available -= words;
1227 fifo->entry[i].address += words * 4;
1228 fifo->entry[i].size -= words;
1229
1230
1231
1232
1233
1234 while (words > 3) {
1235 cvmx_write64_uint32(csr_address, *ptr++);
1236 cvmx_write64_uint32(csr_address, *ptr++);
1237 cvmx_write64_uint32(csr_address, *ptr++);
1238 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1239 words -= 3;
1240 }
1241 cvmx_write64_uint32(csr_address, *ptr++);
1242 if (--words) {
1243 cvmx_write64_uint32(csr_address, *ptr++);
1244 if (--words)
1245 cvmx_write64_uint32(csr_address, *ptr++);
1246 }
1247 cvmx_read64_uint64(CVMX_USBNX_DMA0_INB_CHN0(usb->index));
1248 }
1249 return fifo->head != fifo->tail;
1250 }
1251
1252
1253
1254
1255
1256
1257 static void cvmx_usb_poll_tx_fifo(struct octeon_hcd *usb)
1258 {
1259 if (usb->periodic.head != usb->periodic.tail) {
1260 union cvmx_usbcx_hptxsts tx_status;
1261
1262 tx_status.u32 = cvmx_usb_read_csr32(usb,
1263 CVMX_USBCX_HPTXSTS(usb->index));
1264 if (cvmx_usb_fill_tx_hw(usb, &usb->periodic,
1265 tx_status.s.ptxfspcavail))
1266 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1267 cvmx_usbcx_gintmsk, ptxfempmsk, 1);
1268 else
1269 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1270 cvmx_usbcx_gintmsk, ptxfempmsk, 0);
1271 }
1272
1273 if (usb->nonperiodic.head != usb->nonperiodic.tail) {
1274 union cvmx_usbcx_gnptxsts tx_status;
1275
1276 tx_status.u32 = cvmx_usb_read_csr32(usb,
1277 CVMX_USBCX_GNPTXSTS(usb->index));
1278 if (cvmx_usb_fill_tx_hw(usb, &usb->nonperiodic,
1279 tx_status.s.nptxfspcavail))
1280 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1281 cvmx_usbcx_gintmsk, nptxfempmsk, 1);
1282 else
1283 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1284 cvmx_usbcx_gintmsk, nptxfempmsk, 0);
1285 }
1286 }
1287
1288
1289
1290
1291
1292
1293
1294 static void cvmx_usb_fill_tx_fifo(struct octeon_hcd *usb, int channel)
1295 {
1296 union cvmx_usbcx_hccharx hcchar;
1297 union cvmx_usbcx_hcspltx usbc_hcsplt;
1298 union cvmx_usbcx_hctsizx usbc_hctsiz;
1299 struct cvmx_usb_tx_fifo *fifo;
1300
1301
1302 hcchar.u32 = cvmx_usb_read_csr32(usb,
1303 CVMX_USBCX_HCCHARX(channel, usb->index));
1304 if (hcchar.s.epdir != CVMX_USB_DIRECTION_OUT)
1305 return;
1306
1307
1308 usbc_hcsplt.u32 = cvmx_usb_read_csr32(usb,
1309 CVMX_USBCX_HCSPLTX(channel, usb->index));
1310 if (usbc_hcsplt.s.spltena && usbc_hcsplt.s.compsplt)
1311 return;
1312
1313
1314
1315
1316
1317 usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
1318 CVMX_USBCX_HCTSIZX(channel, usb->index));
1319 if (!usbc_hctsiz.s.xfersize)
1320 return;
1321
1322 if ((hcchar.s.eptype == CVMX_USB_TRANSFER_INTERRUPT) ||
1323 (hcchar.s.eptype == CVMX_USB_TRANSFER_ISOCHRONOUS))
1324 fifo = &usb->periodic;
1325 else
1326 fifo = &usb->nonperiodic;
1327
1328 fifo->entry[fifo->head].channel = channel;
1329 fifo->entry[fifo->head].address =
1330 cvmx_read64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1331 channel * 8);
1332 fifo->entry[fifo->head].size = (usbc_hctsiz.s.xfersize + 3) >> 2;
1333 fifo->head++;
1334 if (fifo->head > MAX_CHANNELS)
1335 fifo->head = 0;
1336
1337 cvmx_usb_poll_tx_fifo(usb);
1338 }
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348 static void cvmx_usb_start_channel_control(struct octeon_hcd *usb,
1349 int channel,
1350 struct cvmx_usb_pipe *pipe)
1351 {
1352 struct usb_hcd *hcd = octeon_to_hcd(usb);
1353 struct device *dev = hcd->self.controller;
1354 struct cvmx_usb_transaction *transaction =
1355 list_first_entry(&pipe->transactions, typeof(*transaction),
1356 node);
1357 struct usb_ctrlrequest *header =
1358 cvmx_phys_to_ptr(transaction->control_header);
1359 int bytes_to_transfer = transaction->buffer_length -
1360 transaction->actual_bytes;
1361 int packets_to_transfer;
1362 union cvmx_usbcx_hctsizx usbc_hctsiz;
1363
1364 usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
1365 CVMX_USBCX_HCTSIZX(channel, usb->index));
1366
1367 switch (transaction->stage) {
1368 case CVMX_USB_STAGE_NON_CONTROL:
1369 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
1370 dev_err(dev, "%s: ERROR - Non control stage\n", __func__);
1371 break;
1372 case CVMX_USB_STAGE_SETUP:
1373 usbc_hctsiz.s.pid = 3;
1374 bytes_to_transfer = sizeof(*header);
1375
1376 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1377 cvmx_usbcx_hccharx, epdir,
1378 CVMX_USB_DIRECTION_OUT);
1379
1380
1381
1382
1383 cvmx_write64_uint64(CVMX_USBNX_DMA0_OUTB_CHN0(usb->index) +
1384 channel * 8,
1385 transaction->control_header);
1386 break;
1387 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
1388 usbc_hctsiz.s.pid = 3;
1389 bytes_to_transfer = 0;
1390
1391 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1392 cvmx_usbcx_hccharx, epdir,
1393 CVMX_USB_DIRECTION_OUT);
1394
1395 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1396 cvmx_usbcx_hcspltx, compsplt, 1);
1397 break;
1398 case CVMX_USB_STAGE_DATA:
1399 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1400 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1401 if (header->bRequestType & USB_DIR_IN)
1402 bytes_to_transfer = 0;
1403 else if (bytes_to_transfer > pipe->max_packet)
1404 bytes_to_transfer = pipe->max_packet;
1405 }
1406 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1407 cvmx_usbcx_hccharx, epdir,
1408 ((header->bRequestType & USB_DIR_IN) ?
1409 CVMX_USB_DIRECTION_IN :
1410 CVMX_USB_DIRECTION_OUT));
1411 break;
1412 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
1413 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1414 if (!(header->bRequestType & USB_DIR_IN))
1415 bytes_to_transfer = 0;
1416 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1417 cvmx_usbcx_hccharx, epdir,
1418 ((header->bRequestType & USB_DIR_IN) ?
1419 CVMX_USB_DIRECTION_IN :
1420 CVMX_USB_DIRECTION_OUT));
1421 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1422 cvmx_usbcx_hcspltx, compsplt, 1);
1423 break;
1424 case CVMX_USB_STAGE_STATUS:
1425 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1426 bytes_to_transfer = 0;
1427 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1428 cvmx_usbcx_hccharx, epdir,
1429 ((header->bRequestType & USB_DIR_IN) ?
1430 CVMX_USB_DIRECTION_OUT :
1431 CVMX_USB_DIRECTION_IN));
1432 break;
1433 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
1434 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1435 bytes_to_transfer = 0;
1436 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1437 cvmx_usbcx_hccharx, epdir,
1438 ((header->bRequestType & USB_DIR_IN) ?
1439 CVMX_USB_DIRECTION_OUT :
1440 CVMX_USB_DIRECTION_IN));
1441 USB_SET_FIELD32(CVMX_USBCX_HCSPLTX(channel, usb->index),
1442 cvmx_usbcx_hcspltx, compsplt, 1);
1443 break;
1444 }
1445
1446
1447
1448
1449
1450 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1451
1452 bytes_to_transfer = MAX_TRANSFER_BYTES / pipe->max_packet;
1453 bytes_to_transfer *= pipe->max_packet;
1454 }
1455
1456
1457
1458
1459
1460 packets_to_transfer = DIV_ROUND_UP(bytes_to_transfer,
1461 pipe->max_packet);
1462 if (packets_to_transfer == 0) {
1463 packets_to_transfer = 1;
1464 } else if ((packets_to_transfer > 1) &&
1465 (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1466
1467
1468
1469
1470
1471 packets_to_transfer = 1;
1472 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1473 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1474
1475
1476
1477
1478 packets_to_transfer = MAX_TRANSFER_PACKETS;
1479 bytes_to_transfer = packets_to_transfer * pipe->max_packet;
1480 }
1481
1482 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1483 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1484
1485 cvmx_usb_write_csr32(usb, CVMX_USBCX_HCTSIZX(channel, usb->index),
1486 usbc_hctsiz.u32);
1487 }
1488
1489
1490
1491
1492
1493
1494
1495
1496 static void cvmx_usb_start_channel(struct octeon_hcd *usb, int channel,
1497 struct cvmx_usb_pipe *pipe)
1498 {
1499 struct cvmx_usb_transaction *transaction =
1500 list_first_entry(&pipe->transactions, typeof(*transaction),
1501 node);
1502
1503
1504 CVMX_SYNCW;
1505
1506
1507 usb->pipe_for_channel[channel] = pipe;
1508 pipe->channel = channel;
1509 pipe->flags |= CVMX_USB_PIPE_FLAGS_SCHEDULED;
1510
1511
1512 usb->idle_hardware_channels &= ~(1 << channel);
1513
1514
1515 {
1516 union cvmx_usbcx_hcintx usbc_hcint;
1517 union cvmx_usbcx_hcintmskx usbc_hcintmsk;
1518 union cvmx_usbcx_haintmsk usbc_haintmsk;
1519
1520
1521 usbc_hcint.u32 = cvmx_usb_read_csr32(usb,
1522 CVMX_USBCX_HCINTX(channel, usb->index));
1523
1524 cvmx_usb_write_csr32(usb,
1525 CVMX_USBCX_HCINTX(channel, usb->index),
1526 usbc_hcint.u32);
1527
1528 usbc_hcintmsk.u32 = 0;
1529 usbc_hcintmsk.s.chhltdmsk = 1;
1530 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1531
1532
1533
1534
1535 usbc_hcintmsk.s.datatglerrmsk = 1;
1536 usbc_hcintmsk.s.frmovrunmsk = 1;
1537 usbc_hcintmsk.s.bblerrmsk = 1;
1538 usbc_hcintmsk.s.xacterrmsk = 1;
1539 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1540
1541
1542
1543
1544 usbc_hcintmsk.s.nyetmsk = 1;
1545 usbc_hcintmsk.s.ackmsk = 1;
1546 }
1547 usbc_hcintmsk.s.nakmsk = 1;
1548 usbc_hcintmsk.s.stallmsk = 1;
1549 usbc_hcintmsk.s.xfercomplmsk = 1;
1550 }
1551 cvmx_usb_write_csr32(usb,
1552 CVMX_USBCX_HCINTMSKX(channel, usb->index),
1553 usbc_hcintmsk.u32);
1554
1555
1556 usbc_haintmsk.u32 = cvmx_usb_read_csr32(usb,
1557 CVMX_USBCX_HAINTMSK(usb->index));
1558 usbc_haintmsk.s.haintmsk |= 1 << channel;
1559 cvmx_usb_write_csr32(usb, CVMX_USBCX_HAINTMSK(usb->index),
1560 usbc_haintmsk.u32);
1561 }
1562
1563
1564 {
1565 u64 reg;
1566 u64 dma_address = transaction->buffer +
1567 transaction->actual_bytes;
1568
1569 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1570 dma_address = transaction->buffer +
1571 transaction->iso_packets[0].offset +
1572 transaction->actual_bytes;
1573
1574 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT)
1575 reg = CVMX_USBNX_DMA0_OUTB_CHN0(usb->index);
1576 else
1577 reg = CVMX_USBNX_DMA0_INB_CHN0(usb->index);
1578 cvmx_write64_uint64(reg + channel * 8, dma_address);
1579 }
1580
1581
1582 {
1583 union cvmx_usbcx_hcspltx usbc_hcsplt = {.u32 = 0};
1584 union cvmx_usbcx_hctsizx usbc_hctsiz = {.u32 = 0};
1585 int packets_to_transfer;
1586 int bytes_to_transfer = transaction->buffer_length -
1587 transaction->actual_bytes;
1588
1589
1590
1591
1592
1593 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
1594 bytes_to_transfer =
1595 transaction->iso_packets[0].length -
1596 transaction->actual_bytes;
1597
1598
1599
1600
1601
1602 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
1603
1604
1605
1606
1607
1608
1609 if ((transaction->stage & 1) == 0) {
1610 if (transaction->type == CVMX_USB_TRANSFER_BULK)
1611 pipe->split_sc_frame =
1612 (usb->frame_number + 1) & 0x7f;
1613 else
1614 pipe->split_sc_frame =
1615 (usb->frame_number + 2) & 0x7f;
1616 } else {
1617 pipe->split_sc_frame = -1;
1618 }
1619
1620 usbc_hcsplt.s.spltena = 1;
1621 usbc_hcsplt.s.hubaddr = pipe->hub_device_addr;
1622 usbc_hcsplt.s.prtaddr = pipe->hub_port;
1623 usbc_hcsplt.s.compsplt = (transaction->stage ==
1624 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE);
1625
1626
1627
1628
1629
1630
1631 if (bytes_to_transfer > pipe->max_packet)
1632 bytes_to_transfer = pipe->max_packet;
1633
1634
1635
1636
1637
1638
1639 if (!usbc_hcsplt.s.compsplt &&
1640 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
1641 (pipe->transfer_type ==
1642 CVMX_USB_TRANSFER_ISOCHRONOUS)) {
1643
1644
1645
1646
1647 pipe->split_sc_frame = -1;
1648
1649
1650
1651
1652 if (transaction->actual_bytes == 0) {
1653
1654
1655
1656
1657 if (bytes_to_transfer <= 188)
1658
1659 usbc_hcsplt.s.xactpos = 3;
1660 else
1661
1662 usbc_hcsplt.s.xactpos = 2;
1663 } else {
1664
1665
1666
1667
1668 if (bytes_to_transfer <= 188)
1669
1670 usbc_hcsplt.s.xactpos = 1;
1671 else
1672
1673 usbc_hcsplt.s.xactpos = 0;
1674 }
1675
1676
1677
1678
1679 if (bytes_to_transfer > 188)
1680 bytes_to_transfer = 188;
1681 }
1682 }
1683
1684
1685
1686
1687
1688
1689 if (bytes_to_transfer > MAX_TRANSFER_BYTES) {
1690
1691
1692
1693
1694 bytes_to_transfer = MAX_TRANSFER_BYTES /
1695 pipe->max_packet;
1696 bytes_to_transfer *= pipe->max_packet;
1697 }
1698
1699
1700
1701
1702
1703 packets_to_transfer =
1704 DIV_ROUND_UP(bytes_to_transfer, pipe->max_packet);
1705 if (packets_to_transfer == 0) {
1706 packets_to_transfer = 1;
1707 } else if ((packets_to_transfer > 1) &&
1708 (usb->init_flags &
1709 CVMX_USB_INITIALIZE_FLAGS_NO_DMA)) {
1710
1711
1712
1713
1714
1715
1716 packets_to_transfer = 1;
1717 bytes_to_transfer = packets_to_transfer *
1718 pipe->max_packet;
1719 } else if (packets_to_transfer > MAX_TRANSFER_PACKETS) {
1720
1721
1722
1723
1724 packets_to_transfer = MAX_TRANSFER_PACKETS;
1725 bytes_to_transfer = packets_to_transfer *
1726 pipe->max_packet;
1727 }
1728
1729 usbc_hctsiz.s.xfersize = bytes_to_transfer;
1730 usbc_hctsiz.s.pktcnt = packets_to_transfer;
1731
1732
1733 usbc_hctsiz.s.pid = cvmx_usb_get_data_pid(pipe);
1734
1735
1736
1737 if (pipe->flags & CVMX_USB_PIPE_FLAGS_NEED_PING)
1738 usbc_hctsiz.s.dopng = 1;
1739
1740 cvmx_usb_write_csr32(usb,
1741 CVMX_USBCX_HCSPLTX(channel, usb->index),
1742 usbc_hcsplt.u32);
1743 cvmx_usb_write_csr32(usb,
1744 CVMX_USBCX_HCTSIZX(channel, usb->index),
1745 usbc_hctsiz.u32);
1746 }
1747
1748
1749 {
1750 union cvmx_usbcx_hccharx usbc_hcchar = {.u32 = 0};
1751
1752
1753
1754
1755
1756 usbc_hcchar.s.oddfrm = usb->frame_number & 1;
1757
1758
1759
1760
1761
1762
1763
1764 if (cvmx_usb_pipe_needs_split(usb, pipe))
1765 usbc_hcchar.s.ec = 1;
1766 else if (pipe->multi_count < 1)
1767 usbc_hcchar.s.ec = 1;
1768 else if (pipe->multi_count > 3)
1769 usbc_hcchar.s.ec = 3;
1770 else
1771 usbc_hcchar.s.ec = pipe->multi_count;
1772
1773
1774 usbc_hcchar.s.devaddr = pipe->device_addr;
1775 usbc_hcchar.s.eptype = transaction->type;
1776 usbc_hcchar.s.lspddev =
1777 (pipe->device_speed == CVMX_USB_SPEED_LOW);
1778 usbc_hcchar.s.epdir = pipe->transfer_dir;
1779 usbc_hcchar.s.epnum = pipe->endpoint_num;
1780 usbc_hcchar.s.mps = pipe->max_packet;
1781 cvmx_usb_write_csr32(usb,
1782 CVMX_USBCX_HCCHARX(channel, usb->index),
1783 usbc_hcchar.u32);
1784 }
1785
1786
1787 switch (transaction->type) {
1788 case CVMX_USB_TRANSFER_CONTROL:
1789 cvmx_usb_start_channel_control(usb, channel, pipe);
1790 break;
1791 case CVMX_USB_TRANSFER_BULK:
1792 case CVMX_USB_TRANSFER_INTERRUPT:
1793 break;
1794 case CVMX_USB_TRANSFER_ISOCHRONOUS:
1795 if (!cvmx_usb_pipe_needs_split(usb, pipe)) {
1796
1797
1798
1799
1800 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
1801 if (pipe->multi_count < 2)
1802 USB_SET_FIELD32(
1803 CVMX_USBCX_HCTSIZX(channel,
1804 usb->index),
1805 cvmx_usbcx_hctsizx, pid, 0);
1806 else
1807 USB_SET_FIELD32(
1808 CVMX_USBCX_HCTSIZX(channel,
1809 usb->index),
1810 cvmx_usbcx_hctsizx, pid, 3);
1811 }
1812 }
1813 break;
1814 }
1815 {
1816 union cvmx_usbcx_hctsizx usbc_hctsiz = { .u32 =
1817 cvmx_usb_read_csr32(usb,
1818 CVMX_USBCX_HCTSIZX(channel,
1819 usb->index))
1820 };
1821 transaction->xfersize = usbc_hctsiz.s.xfersize;
1822 transaction->pktcnt = usbc_hctsiz.s.pktcnt;
1823 }
1824
1825 if (cvmx_usb_pipe_needs_split(usb, pipe))
1826 usb->active_split = transaction;
1827 USB_SET_FIELD32(CVMX_USBCX_HCCHARX(channel, usb->index),
1828 cvmx_usbcx_hccharx, chena, 1);
1829 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
1830 cvmx_usb_fill_tx_fifo(usb, channel);
1831 }
1832
1833
1834
1835
1836
1837
1838
1839
1840 static struct cvmx_usb_pipe *cvmx_usb_find_ready_pipe(struct octeon_hcd *usb,
1841 enum cvmx_usb_transfer xfer_type)
1842 {
1843 struct list_head *list = usb->active_pipes + xfer_type;
1844 u64 current_frame = usb->frame_number;
1845 struct cvmx_usb_pipe *pipe;
1846
1847 list_for_each_entry(pipe, list, node) {
1848 struct cvmx_usb_transaction *t =
1849 list_first_entry(&pipe->transactions, typeof(*t),
1850 node);
1851 if (!(pipe->flags & CVMX_USB_PIPE_FLAGS_SCHEDULED) && t &&
1852 (pipe->next_tx_frame <= current_frame) &&
1853 ((pipe->split_sc_frame == -1) ||
1854 ((((int)current_frame - pipe->split_sc_frame) & 0x7f) <
1855 0x40)) &&
1856 (!usb->active_split || (usb->active_split == t))) {
1857 prefetch(t);
1858 return pipe;
1859 }
1860 }
1861 return NULL;
1862 }
1863
1864 static struct cvmx_usb_pipe *cvmx_usb_next_pipe(struct octeon_hcd *usb,
1865 int is_sof)
1866 {
1867 struct cvmx_usb_pipe *pipe;
1868
1869
1870 if (is_sof) {
1871
1872
1873
1874
1875
1876 pipe = cvmx_usb_find_ready_pipe(usb,
1877 CVMX_USB_TRANSFER_ISOCHRONOUS);
1878 if (pipe)
1879 return pipe;
1880 pipe = cvmx_usb_find_ready_pipe(usb,
1881 CVMX_USB_TRANSFER_INTERRUPT);
1882 if (pipe)
1883 return pipe;
1884 }
1885 pipe = cvmx_usb_find_ready_pipe(usb, CVMX_USB_TRANSFER_CONTROL);
1886 if (pipe)
1887 return pipe;
1888 return cvmx_usb_find_ready_pipe(usb, CVMX_USB_TRANSFER_BULK);
1889 }
1890
1891
1892
1893
1894
1895
1896
1897
1898 static void cvmx_usb_schedule(struct octeon_hcd *usb, int is_sof)
1899 {
1900 int channel;
1901 struct cvmx_usb_pipe *pipe;
1902 int need_sof;
1903 enum cvmx_usb_transfer ttype;
1904
1905 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
1906
1907
1908
1909
1910 union cvmx_usbcx_hfnum hfnum = {
1911 .u32 = cvmx_usb_read_csr32(usb,
1912 CVMX_USBCX_HFNUM(usb->index))
1913 };
1914
1915 union cvmx_usbcx_hfir hfir = {
1916 .u32 = cvmx_usb_read_csr32(usb,
1917 CVMX_USBCX_HFIR(usb->index))
1918 };
1919
1920 if (hfnum.s.frrem < hfir.s.frint / 4)
1921 goto done;
1922 }
1923
1924 while (usb->idle_hardware_channels) {
1925
1926 channel = __fls(usb->idle_hardware_channels);
1927 if (unlikely(channel > 7))
1928 break;
1929
1930 pipe = cvmx_usb_next_pipe(usb, is_sof);
1931 if (!pipe)
1932 break;
1933
1934 cvmx_usb_start_channel(usb, channel, pipe);
1935 }
1936
1937 done:
1938
1939
1940
1941
1942 need_sof = 0;
1943 for (ttype = CVMX_USB_TRANSFER_CONTROL;
1944 ttype <= CVMX_USB_TRANSFER_INTERRUPT; ttype++) {
1945 list_for_each_entry(pipe, &usb->active_pipes[ttype], node) {
1946 if (pipe->next_tx_frame > usb->frame_number) {
1947 need_sof = 1;
1948 break;
1949 }
1950 }
1951 }
1952 USB_SET_FIELD32(CVMX_USBCX_GINTMSK(usb->index),
1953 cvmx_usbcx_gintmsk, sofmsk, need_sof);
1954 }
1955
1956 static void octeon_usb_urb_complete_callback(struct octeon_hcd *usb,
1957 enum cvmx_usb_status status,
1958 struct cvmx_usb_pipe *pipe,
1959 struct cvmx_usb_transaction
1960 *transaction,
1961 int bytes_transferred,
1962 struct urb *urb)
1963 {
1964 struct usb_hcd *hcd = octeon_to_hcd(usb);
1965 struct device *dev = hcd->self.controller;
1966
1967 if (likely(status == CVMX_USB_STATUS_OK))
1968 urb->actual_length = bytes_transferred;
1969 else
1970 urb->actual_length = 0;
1971
1972 urb->hcpriv = NULL;
1973
1974
1975
1976
1977 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1978 int i;
1979
1980
1981
1982
1983 struct cvmx_usb_iso_packet *iso_packet =
1984 (struct cvmx_usb_iso_packet *)urb->setup_packet;
1985
1986 urb->actual_length = 0;
1987 for (i = 0; i < urb->number_of_packets; i++) {
1988 if (iso_packet[i].status == CVMX_USB_STATUS_OK) {
1989 urb->iso_frame_desc[i].status = 0;
1990 urb->iso_frame_desc[i].actual_length =
1991 iso_packet[i].length;
1992 urb->actual_length +=
1993 urb->iso_frame_desc[i].actual_length;
1994 } else {
1995 dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%p transaction=%p size=%d\n",
1996 i, urb->number_of_packets,
1997 iso_packet[i].status, pipe,
1998 transaction, iso_packet[i].length);
1999 urb->iso_frame_desc[i].status = -EREMOTEIO;
2000 }
2001 }
2002
2003 kfree(iso_packet);
2004 urb->setup_packet = NULL;
2005 }
2006
2007 switch (status) {
2008 case CVMX_USB_STATUS_OK:
2009 urb->status = 0;
2010 break;
2011 case CVMX_USB_STATUS_CANCEL:
2012 if (urb->status == 0)
2013 urb->status = -ENOENT;
2014 break;
2015 case CVMX_USB_STATUS_STALL:
2016 dev_dbg(dev, "status=stall pipe=%p transaction=%p size=%d\n",
2017 pipe, transaction, bytes_transferred);
2018 urb->status = -EPIPE;
2019 break;
2020 case CVMX_USB_STATUS_BABBLEERR:
2021 dev_dbg(dev, "status=babble pipe=%p transaction=%p size=%d\n",
2022 pipe, transaction, bytes_transferred);
2023 urb->status = -EPIPE;
2024 break;
2025 case CVMX_USB_STATUS_SHORT:
2026 dev_dbg(dev, "status=short pipe=%p transaction=%p size=%d\n",
2027 pipe, transaction, bytes_transferred);
2028 urb->status = -EREMOTEIO;
2029 break;
2030 case CVMX_USB_STATUS_ERROR:
2031 case CVMX_USB_STATUS_XACTERR:
2032 case CVMX_USB_STATUS_DATATGLERR:
2033 case CVMX_USB_STATUS_FRAMEERR:
2034 dev_dbg(dev, "status=%d pipe=%p transaction=%p size=%d\n",
2035 status, pipe, transaction, bytes_transferred);
2036 urb->status = -EPROTO;
2037 break;
2038 }
2039 usb_hcd_unlink_urb_from_ep(octeon_to_hcd(usb), urb);
2040 spin_unlock(&usb->lock);
2041 usb_hcd_giveback_urb(octeon_to_hcd(usb), urb, urb->status);
2042 spin_lock(&usb->lock);
2043 }
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056 static void cvmx_usb_complete(struct octeon_hcd *usb,
2057 struct cvmx_usb_pipe *pipe,
2058 struct cvmx_usb_transaction *transaction,
2059 enum cvmx_usb_status complete_code)
2060 {
2061
2062 if (usb->active_split == transaction)
2063 usb->active_split = NULL;
2064
2065
2066
2067
2068
2069 if (unlikely(transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)) {
2070
2071 transaction->iso_packets[0].length = transaction->actual_bytes;
2072 transaction->iso_packets[0].status = complete_code;
2073
2074
2075
2076
2077
2078 if ((transaction->iso_number_packets > 1) &&
2079 (complete_code == CVMX_USB_STATUS_OK)) {
2080
2081 transaction->actual_bytes = 0;
2082
2083 transaction->iso_number_packets--;
2084
2085 transaction->iso_packets++;
2086 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2087 return;
2088 }
2089 }
2090
2091
2092 list_del(&transaction->node);
2093 if (list_empty(&pipe->transactions))
2094 list_move_tail(&pipe->node, &usb->idle_pipes);
2095 octeon_usb_urb_complete_callback(usb, complete_code, pipe,
2096 transaction,
2097 transaction->actual_bytes,
2098 transaction->urb);
2099 kfree(transaction);
2100 }
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124 static struct cvmx_usb_transaction *cvmx_usb_submit_transaction(
2125 struct octeon_hcd *usb,
2126 struct cvmx_usb_pipe *pipe,
2127 enum cvmx_usb_transfer type,
2128 u64 buffer,
2129 int buffer_length,
2130 u64 control_header,
2131 int iso_start_frame,
2132 int iso_number_packets,
2133 struct cvmx_usb_iso_packet *iso_packets,
2134 struct urb *urb)
2135 {
2136 struct cvmx_usb_transaction *transaction;
2137
2138 if (unlikely(pipe->transfer_type != type))
2139 return NULL;
2140
2141 transaction = kzalloc(sizeof(*transaction), GFP_ATOMIC);
2142 if (unlikely(!transaction))
2143 return NULL;
2144
2145 transaction->type = type;
2146 transaction->buffer = buffer;
2147 transaction->buffer_length = buffer_length;
2148 transaction->control_header = control_header;
2149
2150 transaction->iso_start_frame = iso_start_frame;
2151 transaction->iso_number_packets = iso_number_packets;
2152 transaction->iso_packets = iso_packets;
2153 transaction->urb = urb;
2154 if (transaction->type == CVMX_USB_TRANSFER_CONTROL)
2155 transaction->stage = CVMX_USB_STAGE_SETUP;
2156 else
2157 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2158
2159 if (!list_empty(&pipe->transactions)) {
2160 list_add_tail(&transaction->node, &pipe->transactions);
2161 } else {
2162 list_add_tail(&transaction->node, &pipe->transactions);
2163 list_move_tail(&pipe->node,
2164 &usb->active_pipes[pipe->transfer_type]);
2165
2166
2167
2168
2169
2170 cvmx_usb_schedule(usb, 0);
2171 }
2172
2173 return transaction;
2174 }
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185 static struct cvmx_usb_transaction *cvmx_usb_submit_bulk(
2186 struct octeon_hcd *usb,
2187 struct cvmx_usb_pipe *pipe,
2188 struct urb *urb)
2189 {
2190 return cvmx_usb_submit_transaction(usb, pipe, CVMX_USB_TRANSFER_BULK,
2191 urb->transfer_dma,
2192 urb->transfer_buffer_length,
2193 0,
2194 0,
2195 0,
2196 NULL,
2197 urb);
2198 }
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209 static struct cvmx_usb_transaction *cvmx_usb_submit_interrupt(
2210 struct octeon_hcd *usb,
2211 struct cvmx_usb_pipe *pipe,
2212 struct urb *urb)
2213 {
2214 return cvmx_usb_submit_transaction(usb, pipe,
2215 CVMX_USB_TRANSFER_INTERRUPT,
2216 urb->transfer_dma,
2217 urb->transfer_buffer_length,
2218 0,
2219 0,
2220 0,
2221 NULL,
2222 urb);
2223 }
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234 static struct cvmx_usb_transaction *cvmx_usb_submit_control(
2235 struct octeon_hcd *usb,
2236 struct cvmx_usb_pipe *pipe,
2237 struct urb *urb)
2238 {
2239 int buffer_length = urb->transfer_buffer_length;
2240 u64 control_header = urb->setup_dma;
2241 struct usb_ctrlrequest *header = cvmx_phys_to_ptr(control_header);
2242
2243 if ((header->bRequestType & USB_DIR_IN) == 0)
2244 buffer_length = le16_to_cpu(header->wLength);
2245
2246 return cvmx_usb_submit_transaction(usb, pipe,
2247 CVMX_USB_TRANSFER_CONTROL,
2248 urb->transfer_dma, buffer_length,
2249 control_header,
2250 0,
2251 0,
2252 NULL,
2253 urb);
2254 }
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265 static struct cvmx_usb_transaction *cvmx_usb_submit_isochronous(
2266 struct octeon_hcd *usb,
2267 struct cvmx_usb_pipe *pipe,
2268 struct urb *urb)
2269 {
2270 struct cvmx_usb_iso_packet *packets;
2271
2272 packets = (struct cvmx_usb_iso_packet *)urb->setup_packet;
2273 return cvmx_usb_submit_transaction(usb, pipe,
2274 CVMX_USB_TRANSFER_ISOCHRONOUS,
2275 urb->transfer_dma,
2276 urb->transfer_buffer_length,
2277 0,
2278 urb->start_frame,
2279 urb->number_of_packets,
2280 packets, urb);
2281 }
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296 static int cvmx_usb_cancel(struct octeon_hcd *usb,
2297 struct cvmx_usb_pipe *pipe,
2298 struct cvmx_usb_transaction *transaction)
2299 {
2300
2301
2302
2303
2304 if (list_first_entry(&pipe->transactions, typeof(*transaction), node) ==
2305 transaction && (pipe->flags & CVMX_USB_PIPE_FLAGS_SCHEDULED)) {
2306 union cvmx_usbcx_hccharx usbc_hcchar;
2307
2308 usb->pipe_for_channel[pipe->channel] = NULL;
2309 pipe->flags &= ~CVMX_USB_PIPE_FLAGS_SCHEDULED;
2310
2311 CVMX_SYNCW;
2312
2313 usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2314 CVMX_USBCX_HCCHARX(pipe->channel,
2315 usb->index));
2316
2317
2318
2319
2320 if (usbc_hcchar.s.chena) {
2321 usbc_hcchar.s.chdis = 1;
2322 cvmx_usb_write_csr32(usb,
2323 CVMX_USBCX_HCCHARX(pipe->channel,
2324 usb->index),
2325 usbc_hcchar.u32);
2326 }
2327 }
2328 cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_CANCEL);
2329 return 0;
2330 }
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341 static int cvmx_usb_cancel_all(struct octeon_hcd *usb,
2342 struct cvmx_usb_pipe *pipe)
2343 {
2344 struct cvmx_usb_transaction *transaction, *next;
2345
2346
2347 list_for_each_entry_safe(transaction, next, &pipe->transactions, node) {
2348 int result = cvmx_usb_cancel(usb, pipe, transaction);
2349
2350 if (unlikely(result != 0))
2351 return result;
2352 }
2353 return 0;
2354 }
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365 static int cvmx_usb_close_pipe(struct octeon_hcd *usb,
2366 struct cvmx_usb_pipe *pipe)
2367 {
2368
2369 if (!list_empty(&pipe->transactions))
2370 return -EBUSY;
2371
2372 list_del(&pipe->node);
2373 kfree(pipe);
2374
2375 return 0;
2376 }
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386 static int cvmx_usb_get_frame_number(struct octeon_hcd *usb)
2387 {
2388 union cvmx_usbcx_hfnum usbc_hfnum;
2389
2390 usbc_hfnum.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2391
2392 return usbc_hfnum.s.frnum;
2393 }
2394
2395 static void cvmx_usb_transfer_control(struct octeon_hcd *usb,
2396 struct cvmx_usb_pipe *pipe,
2397 struct cvmx_usb_transaction *transaction,
2398 union cvmx_usbcx_hccharx usbc_hcchar,
2399 int buffer_space_left,
2400 int bytes_in_last_packet)
2401 {
2402 switch (transaction->stage) {
2403 case CVMX_USB_STAGE_NON_CONTROL:
2404 case CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE:
2405
2406 cvmx_usb_complete(usb, pipe, transaction,
2407 CVMX_USB_STATUS_ERROR);
2408 break;
2409 case CVMX_USB_STAGE_SETUP:
2410 pipe->pid_toggle = 1;
2411 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2412 transaction->stage =
2413 CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE;
2414 } else {
2415 struct usb_ctrlrequest *header =
2416 cvmx_phys_to_ptr(transaction->control_header);
2417 if (header->wLength)
2418 transaction->stage = CVMX_USB_STAGE_DATA;
2419 else
2420 transaction->stage = CVMX_USB_STAGE_STATUS;
2421 }
2422 break;
2423 case CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE:
2424 {
2425 struct usb_ctrlrequest *header =
2426 cvmx_phys_to_ptr(transaction->control_header);
2427 if (header->wLength)
2428 transaction->stage = CVMX_USB_STAGE_DATA;
2429 else
2430 transaction->stage = CVMX_USB_STAGE_STATUS;
2431 }
2432 break;
2433 case CVMX_USB_STAGE_DATA:
2434 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2435 transaction->stage = CVMX_USB_STAGE_DATA_SPLIT_COMPLETE;
2436
2437
2438
2439
2440
2441
2442 if (!usbc_hcchar.s.epdir) {
2443 if (buffer_space_left < pipe->max_packet)
2444 transaction->actual_bytes +=
2445 buffer_space_left;
2446 else
2447 transaction->actual_bytes +=
2448 pipe->max_packet;
2449 }
2450 } else if ((buffer_space_left == 0) ||
2451 (bytes_in_last_packet < pipe->max_packet)) {
2452 pipe->pid_toggle = 1;
2453 transaction->stage = CVMX_USB_STAGE_STATUS;
2454 }
2455 break;
2456 case CVMX_USB_STAGE_DATA_SPLIT_COMPLETE:
2457 if ((buffer_space_left == 0) ||
2458 (bytes_in_last_packet < pipe->max_packet)) {
2459 pipe->pid_toggle = 1;
2460 transaction->stage = CVMX_USB_STAGE_STATUS;
2461 } else {
2462 transaction->stage = CVMX_USB_STAGE_DATA;
2463 }
2464 break;
2465 case CVMX_USB_STAGE_STATUS:
2466 if (cvmx_usb_pipe_needs_split(usb, pipe))
2467 transaction->stage =
2468 CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE;
2469 else
2470 cvmx_usb_complete(usb, pipe, transaction,
2471 CVMX_USB_STATUS_OK);
2472 break;
2473 case CVMX_USB_STAGE_STATUS_SPLIT_COMPLETE:
2474 cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2475 break;
2476 }
2477 }
2478
2479 static void cvmx_usb_transfer_bulk(struct octeon_hcd *usb,
2480 struct cvmx_usb_pipe *pipe,
2481 struct cvmx_usb_transaction *transaction,
2482 union cvmx_usbcx_hcintx usbc_hcint,
2483 int buffer_space_left,
2484 int bytes_in_last_packet)
2485 {
2486
2487
2488
2489
2490
2491 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2492 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL)
2493 transaction->stage =
2494 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2495 else if (buffer_space_left &&
2496 (bytes_in_last_packet == pipe->max_packet))
2497 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2498 else
2499 cvmx_usb_complete(usb, pipe, transaction,
2500 CVMX_USB_STATUS_OK);
2501 } else {
2502 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2503 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) &&
2504 (usbc_hcint.s.nak))
2505 pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
2506 if (!buffer_space_left ||
2507 (bytes_in_last_packet < pipe->max_packet))
2508 cvmx_usb_complete(usb, pipe, transaction,
2509 CVMX_USB_STATUS_OK);
2510 }
2511 }
2512
2513 static void cvmx_usb_transfer_intr(struct octeon_hcd *usb,
2514 struct cvmx_usb_pipe *pipe,
2515 struct cvmx_usb_transaction *transaction,
2516 int buffer_space_left,
2517 int bytes_in_last_packet)
2518 {
2519 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2520 if (transaction->stage == CVMX_USB_STAGE_NON_CONTROL) {
2521 transaction->stage =
2522 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2523 } else if (buffer_space_left &&
2524 (bytes_in_last_packet == pipe->max_packet)) {
2525 transaction->stage = CVMX_USB_STAGE_NON_CONTROL;
2526 } else {
2527 pipe->next_tx_frame += pipe->interval;
2528 cvmx_usb_complete(usb, pipe, transaction,
2529 CVMX_USB_STATUS_OK);
2530 }
2531 } else if (!buffer_space_left ||
2532 (bytes_in_last_packet < pipe->max_packet)) {
2533 pipe->next_tx_frame += pipe->interval;
2534 cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2535 }
2536 }
2537
2538 static void cvmx_usb_transfer_isoc(struct octeon_hcd *usb,
2539 struct cvmx_usb_pipe *pipe,
2540 struct cvmx_usb_transaction *transaction,
2541 int buffer_space_left,
2542 int bytes_in_last_packet,
2543 int bytes_this_transfer)
2544 {
2545 if (cvmx_usb_pipe_needs_split(usb, pipe)) {
2546
2547
2548
2549
2550
2551
2552 if (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT) {
2553
2554
2555
2556
2557
2558 if (!buffer_space_left || (bytes_this_transfer < 188)) {
2559 pipe->next_tx_frame += pipe->interval;
2560 cvmx_usb_complete(usb, pipe, transaction,
2561 CVMX_USB_STATUS_OK);
2562 }
2563 return;
2564 }
2565 if (transaction->stage ==
2566 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE) {
2567
2568
2569
2570
2571 if ((buffer_space_left == 0) ||
2572 (bytes_in_last_packet < pipe->max_packet)) {
2573 pipe->next_tx_frame += pipe->interval;
2574 cvmx_usb_complete(usb, pipe, transaction,
2575 CVMX_USB_STATUS_OK);
2576 }
2577 } else {
2578 transaction->stage =
2579 CVMX_USB_STAGE_NON_CONTROL_SPLIT_COMPLETE;
2580 }
2581 } else {
2582 pipe->next_tx_frame += pipe->interval;
2583 cvmx_usb_complete(usb, pipe, transaction, CVMX_USB_STATUS_OK);
2584 }
2585 }
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595 static int cvmx_usb_poll_channel(struct octeon_hcd *usb, int channel)
2596 {
2597 struct usb_hcd *hcd = octeon_to_hcd(usb);
2598 struct device *dev = hcd->self.controller;
2599 union cvmx_usbcx_hcintx usbc_hcint;
2600 union cvmx_usbcx_hctsizx usbc_hctsiz;
2601 union cvmx_usbcx_hccharx usbc_hcchar;
2602 struct cvmx_usb_pipe *pipe;
2603 struct cvmx_usb_transaction *transaction;
2604 int bytes_this_transfer;
2605 int bytes_in_last_packet;
2606 int packets_processed;
2607 int buffer_space_left;
2608
2609
2610 usbc_hcint.u32 = cvmx_usb_read_csr32(usb,
2611 CVMX_USBCX_HCINTX(channel, usb->index));
2612
2613 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA) {
2614 usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2615 CVMX_USBCX_HCCHARX(channel,
2616 usb->index));
2617
2618 if (usbc_hcchar.s.chena && usbc_hcchar.s.chdis) {
2619
2620
2621
2622
2623
2624 cvmx_usb_write_csr32(usb,
2625 CVMX_USBCX_HCCHARX(channel,
2626 usb->index),
2627 usbc_hcchar.u32);
2628 return 0;
2629 }
2630
2631
2632
2633
2634
2635 if (!usbc_hcint.s.chhltd) {
2636 if (usbc_hcchar.s.chena) {
2637 union cvmx_usbcx_hcintmskx hcintmsk;
2638
2639 hcintmsk.u32 = 0;
2640 hcintmsk.s.chhltdmsk = 1;
2641 cvmx_usb_write_csr32(usb,
2642 CVMX_USBCX_HCINTMSKX(channel, usb->index),
2643 hcintmsk.u32);
2644 usbc_hcchar.s.chdis = 1;
2645 cvmx_usb_write_csr32(usb,
2646 CVMX_USBCX_HCCHARX(channel, usb->index),
2647 usbc_hcchar.u32);
2648 return 0;
2649 } else if (usbc_hcint.s.xfercompl) {
2650
2651
2652
2653
2654 } else {
2655 dev_err(dev, "USB%d: Channel %d interrupt without halt\n",
2656 usb->index, channel);
2657 return 0;
2658 }
2659 }
2660 } else {
2661
2662
2663
2664
2665 if (!usbc_hcint.s.chhltd)
2666 return 0;
2667 }
2668
2669
2670 cvmx_usb_write_csr32(usb, CVMX_USBCX_HCINTMSKX(channel, usb->index), 0);
2671 usb->idle_hardware_channels |= (1 << channel);
2672
2673
2674 pipe = usb->pipe_for_channel[channel];
2675 prefetch(pipe);
2676 if (!pipe)
2677 return 0;
2678 transaction = list_first_entry(&pipe->transactions,
2679 typeof(*transaction),
2680 node);
2681 prefetch(transaction);
2682
2683
2684
2685
2686
2687 usb->pipe_for_channel[channel] = NULL;
2688 pipe->flags &= ~CVMX_USB_PIPE_FLAGS_SCHEDULED;
2689
2690
2691
2692
2693
2694 usbc_hcchar.u32 = cvmx_usb_read_csr32(usb,
2695 CVMX_USBCX_HCCHARX(channel, usb->index));
2696 usbc_hctsiz.u32 = cvmx_usb_read_csr32(usb,
2697 CVMX_USBCX_HCTSIZX(channel, usb->index));
2698
2699
2700
2701
2702
2703 packets_processed = transaction->pktcnt - usbc_hctsiz.s.pktcnt;
2704 if (usbc_hcchar.s.epdir) {
2705
2706
2707
2708
2709
2710
2711 bytes_this_transfer = transaction->xfersize -
2712 usbc_hctsiz.s.xfersize;
2713 } else {
2714
2715
2716
2717
2718
2719
2720 bytes_this_transfer = packets_processed * usbc_hcchar.s.mps;
2721
2722
2723
2724
2725 if (bytes_this_transfer > transaction->xfersize)
2726 bytes_this_transfer = transaction->xfersize;
2727 }
2728
2729 if (packets_processed)
2730 bytes_in_last_packet = bytes_this_transfer -
2731 (packets_processed - 1) * usbc_hcchar.s.mps;
2732 else
2733 bytes_in_last_packet = bytes_this_transfer;
2734
2735
2736
2737
2738
2739
2740 if ((transaction->stage == CVMX_USB_STAGE_SETUP) ||
2741 (transaction->stage == CVMX_USB_STAGE_SETUP_SPLIT_COMPLETE))
2742 bytes_this_transfer = 0;
2743
2744
2745
2746
2747
2748
2749 transaction->actual_bytes += bytes_this_transfer;
2750 if (transaction->type == CVMX_USB_TRANSFER_ISOCHRONOUS)
2751 buffer_space_left = transaction->iso_packets[0].length -
2752 transaction->actual_bytes;
2753 else
2754 buffer_space_left = transaction->buffer_length -
2755 transaction->actual_bytes;
2756
2757
2758
2759
2760
2761 pipe->pid_toggle = !(usbc_hctsiz.s.pid == 0);
2762
2763
2764
2765
2766
2767
2768 if ((pipe->device_speed == CVMX_USB_SPEED_HIGH) &&
2769 (pipe->transfer_type == CVMX_USB_TRANSFER_BULK) &&
2770 (pipe->transfer_dir == CVMX_USB_DIRECTION_OUT))
2771 pipe->flags |= CVMX_USB_PIPE_FLAGS_NEED_PING;
2772
2773 if (WARN_ON_ONCE(bytes_this_transfer < 0)) {
2774
2775
2776
2777
2778
2779 cvmx_usb_complete(usb, pipe, transaction,
2780 CVMX_USB_STATUS_ERROR);
2781 return 0;
2782 }
2783
2784 if (usbc_hcint.s.stall) {
2785
2786
2787
2788
2789
2790
2791 pipe->pid_toggle = 0;
2792 cvmx_usb_complete(usb, pipe, transaction,
2793 CVMX_USB_STATUS_STALL);
2794 } else if (usbc_hcint.s.xacterr) {
2795
2796
2797
2798
2799
2800 cvmx_usb_complete(usb, pipe, transaction,
2801 CVMX_USB_STATUS_XACTERR);
2802 } else if (usbc_hcint.s.bblerr) {
2803
2804 cvmx_usb_complete(usb, pipe, transaction,
2805 CVMX_USB_STATUS_BABBLEERR);
2806 } else if (usbc_hcint.s.datatglerr) {
2807
2808 cvmx_usb_complete(usb, pipe, transaction,
2809 CVMX_USB_STATUS_DATATGLERR);
2810 } else if (usbc_hcint.s.nyet) {
2811
2812
2813
2814
2815
2816
2817 if (!cvmx_usb_pipe_needs_split(usb, pipe)) {
2818 transaction->retries = 0;
2819
2820
2821
2822
2823 if ((buffer_space_left == 0) ||
2824 (bytes_in_last_packet < pipe->max_packet))
2825 cvmx_usb_complete(usb, pipe,
2826 transaction,
2827 CVMX_USB_STATUS_OK);
2828 } else {
2829
2830
2831
2832
2833
2834 transaction->retries++;
2835 if ((transaction->retries & 0x3) == 0) {
2836
2837
2838
2839
2840 transaction->stage &= ~1;
2841 pipe->split_sc_frame = -1;
2842 }
2843 }
2844 } else if (usbc_hcint.s.ack) {
2845 transaction->retries = 0;
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857 pipe->flags &= ~CVMX_USB_PIPE_FLAGS_NEED_PING;
2858
2859 switch (transaction->type) {
2860 case CVMX_USB_TRANSFER_CONTROL:
2861 cvmx_usb_transfer_control(usb, pipe, transaction,
2862 usbc_hcchar,
2863 buffer_space_left,
2864 bytes_in_last_packet);
2865 break;
2866 case CVMX_USB_TRANSFER_BULK:
2867 cvmx_usb_transfer_bulk(usb, pipe, transaction,
2868 usbc_hcint, buffer_space_left,
2869 bytes_in_last_packet);
2870 break;
2871 case CVMX_USB_TRANSFER_INTERRUPT:
2872 cvmx_usb_transfer_intr(usb, pipe, transaction,
2873 buffer_space_left,
2874 bytes_in_last_packet);
2875 break;
2876 case CVMX_USB_TRANSFER_ISOCHRONOUS:
2877 cvmx_usb_transfer_isoc(usb, pipe, transaction,
2878 buffer_space_left,
2879 bytes_in_last_packet,
2880 bytes_this_transfer);
2881 break;
2882 }
2883 } else if (usbc_hcint.s.nak) {
2884
2885
2886
2887 if (usb->active_split == transaction)
2888 usb->active_split = NULL;
2889
2890
2891
2892
2893
2894
2895 transaction->retries = 0;
2896 transaction->stage &= ~1;
2897 pipe->next_tx_frame += pipe->interval;
2898 if (pipe->next_tx_frame < usb->frame_number)
2899 pipe->next_tx_frame = usb->frame_number +
2900 pipe->interval -
2901 (usb->frame_number - pipe->next_tx_frame) %
2902 pipe->interval;
2903 } else {
2904 struct cvmx_usb_port_status port;
2905
2906 port = cvmx_usb_get_status(usb);
2907 if (port.port_enabled) {
2908
2909 transaction->retries++;
2910 } else {
2911
2912
2913
2914
2915 cvmx_usb_complete(usb, pipe, transaction,
2916 CVMX_USB_STATUS_ERROR);
2917 }
2918 }
2919 return 0;
2920 }
2921
2922 static void octeon_usb_port_callback(struct octeon_hcd *usb)
2923 {
2924 spin_unlock(&usb->lock);
2925 usb_hcd_poll_rh_status(octeon_to_hcd(usb));
2926 spin_lock(&usb->lock);
2927 }
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939 static int cvmx_usb_poll(struct octeon_hcd *usb)
2940 {
2941 union cvmx_usbcx_hfnum usbc_hfnum;
2942 union cvmx_usbcx_gintsts usbc_gintsts;
2943
2944 prefetch_range(usb, sizeof(*usb));
2945
2946
2947 usbc_hfnum.u32 = cvmx_usb_read_csr32(usb, CVMX_USBCX_HFNUM(usb->index));
2948 if ((usb->frame_number & 0x3fff) > usbc_hfnum.s.frnum)
2949 usb->frame_number += 0x4000;
2950 usb->frame_number &= ~0x3fffull;
2951 usb->frame_number |= usbc_hfnum.s.frnum;
2952
2953
2954 usbc_gintsts.u32 = cvmx_usb_read_csr32(usb,
2955 CVMX_USBCX_GINTSTS(usb->index));
2956
2957
2958 cvmx_usb_write_csr32(usb, CVMX_USBCX_GINTSTS(usb->index),
2959 usbc_gintsts.u32);
2960
2961 if (usbc_gintsts.s.rxflvl) {
2962
2963
2964
2965
2966
2967
2968
2969 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
2970 cvmx_usb_poll_rx_fifo(usb);
2971 }
2972 if (usbc_gintsts.s.ptxfemp || usbc_gintsts.s.nptxfemp) {
2973
2974 if (usb->init_flags & CVMX_USB_INITIALIZE_FLAGS_NO_DMA)
2975 cvmx_usb_poll_tx_fifo(usb);
2976 }
2977 if (usbc_gintsts.s.disconnint || usbc_gintsts.s.prtint) {
2978 union cvmx_usbcx_hprt usbc_hprt;
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993 octeon_usb_port_callback(usb);
2994
2995 usbc_hprt.u32 =
2996 cvmx_usb_read_csr32(usb, CVMX_USBCX_HPRT(usb->index));
2997 usbc_hprt.s.prtena = 0;
2998 cvmx_usb_write_csr32(usb, CVMX_USBCX_HPRT(usb->index),
2999 usbc_hprt.u32);
3000 }
3001 if (usbc_gintsts.s.hchint) {
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014 union cvmx_usbcx_haint usbc_haint;
3015
3016 usbc_haint.u32 = cvmx_usb_read_csr32(usb,
3017 CVMX_USBCX_HAINT(usb->index));
3018 while (usbc_haint.u32) {
3019 int channel;
3020
3021 channel = __fls(usbc_haint.u32);
3022 cvmx_usb_poll_channel(usb, channel);
3023 usbc_haint.u32 ^= 1 << channel;
3024 }
3025 }
3026
3027 cvmx_usb_schedule(usb, usbc_gintsts.s.sof);
3028
3029 return 0;
3030 }
3031
3032
3033 static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
3034 {
3035 return (struct octeon_hcd *)(hcd->hcd_priv);
3036 }
3037
3038 static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
3039 {
3040 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3041 unsigned long flags;
3042
3043 spin_lock_irqsave(&usb->lock, flags);
3044 cvmx_usb_poll(usb);
3045 spin_unlock_irqrestore(&usb->lock, flags);
3046 return IRQ_HANDLED;
3047 }
3048
3049 static int octeon_usb_start(struct usb_hcd *hcd)
3050 {
3051 hcd->state = HC_STATE_RUNNING;
3052 return 0;
3053 }
3054
3055 static void octeon_usb_stop(struct usb_hcd *hcd)
3056 {
3057 hcd->state = HC_STATE_HALT;
3058 }
3059
3060 static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
3061 {
3062 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3063
3064 return cvmx_usb_get_frame_number(usb);
3065 }
3066
3067 static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
3068 struct urb *urb,
3069 gfp_t mem_flags)
3070 {
3071 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3072 struct device *dev = hcd->self.controller;
3073 struct cvmx_usb_transaction *transaction = NULL;
3074 struct cvmx_usb_pipe *pipe;
3075 unsigned long flags;
3076 struct cvmx_usb_iso_packet *iso_packet;
3077 struct usb_host_endpoint *ep = urb->ep;
3078 int rc;
3079
3080 urb->status = 0;
3081 spin_lock_irqsave(&usb->lock, flags);
3082
3083 rc = usb_hcd_link_urb_to_ep(hcd, urb);
3084 if (rc) {
3085 spin_unlock_irqrestore(&usb->lock, flags);
3086 return rc;
3087 }
3088
3089 if (!ep->hcpriv) {
3090 enum cvmx_usb_transfer transfer_type;
3091 enum cvmx_usb_speed speed;
3092 int split_device = 0;
3093 int split_port = 0;
3094
3095 switch (usb_pipetype(urb->pipe)) {
3096 case PIPE_ISOCHRONOUS:
3097 transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
3098 break;
3099 case PIPE_INTERRUPT:
3100 transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
3101 break;
3102 case PIPE_CONTROL:
3103 transfer_type = CVMX_USB_TRANSFER_CONTROL;
3104 break;
3105 default:
3106 transfer_type = CVMX_USB_TRANSFER_BULK;
3107 break;
3108 }
3109 switch (urb->dev->speed) {
3110 case USB_SPEED_LOW:
3111 speed = CVMX_USB_SPEED_LOW;
3112 break;
3113 case USB_SPEED_FULL:
3114 speed = CVMX_USB_SPEED_FULL;
3115 break;
3116 default:
3117 speed = CVMX_USB_SPEED_HIGH;
3118 break;
3119 }
3120
3121
3122
3123
3124
3125 if (speed != CVMX_USB_SPEED_HIGH) {
3126
3127
3128
3129
3130 struct usb_device *dev = urb->dev;
3131
3132 while (dev->parent) {
3133
3134
3135
3136
3137 if (dev->parent->speed == USB_SPEED_HIGH) {
3138 split_device = dev->parent->devnum;
3139 split_port = dev->portnum;
3140 break;
3141 }
3142
3143
3144
3145
3146
3147
3148 dev = dev->parent;
3149 }
3150 }
3151 pipe = cvmx_usb_open_pipe(usb, usb_pipedevice(urb->pipe),
3152 usb_pipeendpoint(urb->pipe), speed,
3153 le16_to_cpu(ep->desc.wMaxPacketSize)
3154 & 0x7ff,
3155 transfer_type,
3156 usb_pipein(urb->pipe) ?
3157 CVMX_USB_DIRECTION_IN :
3158 CVMX_USB_DIRECTION_OUT,
3159 urb->interval,
3160 (le16_to_cpu(ep->desc.wMaxPacketSize)
3161 >> 11) & 0x3,
3162 split_device, split_port);
3163 if (!pipe) {
3164 usb_hcd_unlink_urb_from_ep(hcd, urb);
3165 spin_unlock_irqrestore(&usb->lock, flags);
3166 dev_dbg(dev, "Failed to create pipe\n");
3167 return -ENOMEM;
3168 }
3169 ep->hcpriv = pipe;
3170 } else {
3171 pipe = ep->hcpriv;
3172 }
3173
3174 switch (usb_pipetype(urb->pipe)) {
3175 case PIPE_ISOCHRONOUS:
3176 dev_dbg(dev, "Submit isochronous to %d.%d\n",
3177 usb_pipedevice(urb->pipe),
3178 usb_pipeendpoint(urb->pipe));
3179
3180
3181
3182
3183 iso_packet = kmalloc_array(urb->number_of_packets,
3184 sizeof(struct cvmx_usb_iso_packet),
3185 GFP_ATOMIC);
3186 if (iso_packet) {
3187 int i;
3188
3189 for (i = 0; i < urb->number_of_packets; i++) {
3190 iso_packet[i].offset =
3191 urb->iso_frame_desc[i].offset;
3192 iso_packet[i].length =
3193 urb->iso_frame_desc[i].length;
3194 iso_packet[i].status = CVMX_USB_STATUS_ERROR;
3195 }
3196
3197
3198
3199
3200
3201 urb->setup_packet = (char *)iso_packet;
3202 transaction = cvmx_usb_submit_isochronous(usb,
3203 pipe, urb);
3204
3205
3206
3207
3208 if (!transaction) {
3209 urb->setup_packet = NULL;
3210 kfree(iso_packet);
3211 }
3212 }
3213 break;
3214 case PIPE_INTERRUPT:
3215 dev_dbg(dev, "Submit interrupt to %d.%d\n",
3216 usb_pipedevice(urb->pipe),
3217 usb_pipeendpoint(urb->pipe));
3218 transaction = cvmx_usb_submit_interrupt(usb, pipe, urb);
3219 break;
3220 case PIPE_CONTROL:
3221 dev_dbg(dev, "Submit control to %d.%d\n",
3222 usb_pipedevice(urb->pipe),
3223 usb_pipeendpoint(urb->pipe));
3224 transaction = cvmx_usb_submit_control(usb, pipe, urb);
3225 break;
3226 case PIPE_BULK:
3227 dev_dbg(dev, "Submit bulk to %d.%d\n",
3228 usb_pipedevice(urb->pipe),
3229 usb_pipeendpoint(urb->pipe));
3230 transaction = cvmx_usb_submit_bulk(usb, pipe, urb);
3231 break;
3232 }
3233 if (!transaction) {
3234 usb_hcd_unlink_urb_from_ep(hcd, urb);
3235 spin_unlock_irqrestore(&usb->lock, flags);
3236 dev_dbg(dev, "Failed to submit\n");
3237 return -ENOMEM;
3238 }
3239 urb->hcpriv = transaction;
3240 spin_unlock_irqrestore(&usb->lock, flags);
3241 return 0;
3242 }
3243
3244 static int octeon_usb_urb_dequeue(struct usb_hcd *hcd,
3245 struct urb *urb,
3246 int status)
3247 {
3248 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3249 unsigned long flags;
3250 int rc;
3251
3252 if (!urb->dev)
3253 return -EINVAL;
3254
3255 spin_lock_irqsave(&usb->lock, flags);
3256
3257 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
3258 if (rc)
3259 goto out;
3260
3261 urb->status = status;
3262 cvmx_usb_cancel(usb, urb->ep->hcpriv, urb->hcpriv);
3263
3264 out:
3265 spin_unlock_irqrestore(&usb->lock, flags);
3266
3267 return rc;
3268 }
3269
3270 static void octeon_usb_endpoint_disable(struct usb_hcd *hcd,
3271 struct usb_host_endpoint *ep)
3272 {
3273 struct device *dev = hcd->self.controller;
3274
3275 if (ep->hcpriv) {
3276 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3277 struct cvmx_usb_pipe *pipe = ep->hcpriv;
3278 unsigned long flags;
3279
3280 spin_lock_irqsave(&usb->lock, flags);
3281 cvmx_usb_cancel_all(usb, pipe);
3282 if (cvmx_usb_close_pipe(usb, pipe))
3283 dev_dbg(dev, "Closing pipe %p failed\n", pipe);
3284 spin_unlock_irqrestore(&usb->lock, flags);
3285 ep->hcpriv = NULL;
3286 }
3287 }
3288
3289 static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
3290 {
3291 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3292 struct cvmx_usb_port_status port_status;
3293 unsigned long flags;
3294
3295 spin_lock_irqsave(&usb->lock, flags);
3296 port_status = cvmx_usb_get_status(usb);
3297 spin_unlock_irqrestore(&usb->lock, flags);
3298 buf[0] = port_status.connect_change << 1;
3299
3300 return buf[0] != 0;
3301 }
3302
3303 static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3304 u16 wIndex, char *buf, u16 wLength)
3305 {
3306 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3307 struct device *dev = hcd->self.controller;
3308 struct cvmx_usb_port_status usb_port_status;
3309 int port_status;
3310 struct usb_hub_descriptor *desc;
3311 unsigned long flags;
3312
3313 switch (typeReq) {
3314 case ClearHubFeature:
3315 dev_dbg(dev, "ClearHubFeature\n");
3316 switch (wValue) {
3317 case C_HUB_LOCAL_POWER:
3318 case C_HUB_OVER_CURRENT:
3319
3320 break;
3321 default:
3322 return -EINVAL;
3323 }
3324 break;
3325 case ClearPortFeature:
3326 dev_dbg(dev, "ClearPortFeature\n");
3327 if (wIndex != 1) {
3328 dev_dbg(dev, " INVALID\n");
3329 return -EINVAL;
3330 }
3331
3332 switch (wValue) {
3333 case USB_PORT_FEAT_ENABLE:
3334 dev_dbg(dev, " ENABLE\n");
3335 spin_lock_irqsave(&usb->lock, flags);
3336 cvmx_usb_disable(usb);
3337 spin_unlock_irqrestore(&usb->lock, flags);
3338 break;
3339 case USB_PORT_FEAT_SUSPEND:
3340 dev_dbg(dev, " SUSPEND\n");
3341
3342 break;
3343 case USB_PORT_FEAT_POWER:
3344 dev_dbg(dev, " POWER\n");
3345
3346 break;
3347 case USB_PORT_FEAT_INDICATOR:
3348 dev_dbg(dev, " INDICATOR\n");
3349
3350 break;
3351 case USB_PORT_FEAT_C_CONNECTION:
3352 dev_dbg(dev, " C_CONNECTION\n");
3353
3354 spin_lock_irqsave(&usb->lock, flags);
3355 usb->port_status = cvmx_usb_get_status(usb);
3356 spin_unlock_irqrestore(&usb->lock, flags);
3357 break;
3358 case USB_PORT_FEAT_C_RESET:
3359 dev_dbg(dev, " C_RESET\n");
3360
3361
3362
3363 spin_lock_irqsave(&usb->lock, flags);
3364 usb->port_status = cvmx_usb_get_status(usb);
3365 spin_unlock_irqrestore(&usb->lock, flags);
3366 break;
3367 case USB_PORT_FEAT_C_ENABLE:
3368 dev_dbg(dev, " C_ENABLE\n");
3369
3370
3371
3372
3373 spin_lock_irqsave(&usb->lock, flags);
3374 usb->port_status = cvmx_usb_get_status(usb);
3375 spin_unlock_irqrestore(&usb->lock, flags);
3376 break;
3377 case USB_PORT_FEAT_C_SUSPEND:
3378 dev_dbg(dev, " C_SUSPEND\n");
3379
3380
3381
3382
3383
3384 break;
3385 case USB_PORT_FEAT_C_OVER_CURRENT:
3386 dev_dbg(dev, " C_OVER_CURRENT\n");
3387
3388 spin_lock_irqsave(&usb->lock, flags);
3389 usb->port_status = cvmx_usb_get_status(usb);
3390 spin_unlock_irqrestore(&usb->lock, flags);
3391 break;
3392 default:
3393 dev_dbg(dev, " UNKNOWN\n");
3394 return -EINVAL;
3395 }
3396 break;
3397 case GetHubDescriptor:
3398 dev_dbg(dev, "GetHubDescriptor\n");
3399 desc = (struct usb_hub_descriptor *)buf;
3400 desc->bDescLength = 9;
3401 desc->bDescriptorType = 0x29;
3402 desc->bNbrPorts = 1;
3403 desc->wHubCharacteristics = cpu_to_le16(0x08);
3404 desc->bPwrOn2PwrGood = 1;
3405 desc->bHubContrCurrent = 0;
3406 desc->u.hs.DeviceRemovable[0] = 0;
3407 desc->u.hs.DeviceRemovable[1] = 0xff;
3408 break;
3409 case GetHubStatus:
3410 dev_dbg(dev, "GetHubStatus\n");
3411 *(__le32 *)buf = 0;
3412 break;
3413 case GetPortStatus:
3414 dev_dbg(dev, "GetPortStatus\n");
3415 if (wIndex != 1) {
3416 dev_dbg(dev, " INVALID\n");
3417 return -EINVAL;
3418 }
3419
3420 spin_lock_irqsave(&usb->lock, flags);
3421 usb_port_status = cvmx_usb_get_status(usb);
3422 spin_unlock_irqrestore(&usb->lock, flags);
3423 port_status = 0;
3424
3425 if (usb_port_status.connect_change) {
3426 port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
3427 dev_dbg(dev, " C_CONNECTION\n");
3428 }
3429
3430 if (usb_port_status.port_enabled) {
3431 port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
3432 dev_dbg(dev, " C_ENABLE\n");
3433 }
3434
3435 if (usb_port_status.connected) {
3436 port_status |= (1 << USB_PORT_FEAT_CONNECTION);
3437 dev_dbg(dev, " CONNECTION\n");
3438 }
3439
3440 if (usb_port_status.port_enabled) {
3441 port_status |= (1 << USB_PORT_FEAT_ENABLE);
3442 dev_dbg(dev, " ENABLE\n");
3443 }
3444
3445 if (usb_port_status.port_over_current) {
3446 port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
3447 dev_dbg(dev, " OVER_CURRENT\n");
3448 }
3449
3450 if (usb_port_status.port_powered) {
3451 port_status |= (1 << USB_PORT_FEAT_POWER);
3452 dev_dbg(dev, " POWER\n");
3453 }
3454
3455 if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
3456 port_status |= USB_PORT_STAT_HIGH_SPEED;
3457 dev_dbg(dev, " HIGHSPEED\n");
3458 } else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
3459 port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
3460 dev_dbg(dev, " LOWSPEED\n");
3461 }
3462
3463 *((__le32 *)buf) = cpu_to_le32(port_status);
3464 break;
3465 case SetHubFeature:
3466 dev_dbg(dev, "SetHubFeature\n");
3467
3468 break;
3469 case SetPortFeature:
3470 dev_dbg(dev, "SetPortFeature\n");
3471 if (wIndex != 1) {
3472 dev_dbg(dev, " INVALID\n");
3473 return -EINVAL;
3474 }
3475
3476 switch (wValue) {
3477 case USB_PORT_FEAT_SUSPEND:
3478 dev_dbg(dev, " SUSPEND\n");
3479 return -EINVAL;
3480 case USB_PORT_FEAT_POWER:
3481 dev_dbg(dev, " POWER\n");
3482
3483
3484
3485 spin_lock_irqsave(&usb->lock, flags);
3486 USB_SET_FIELD32(CVMX_USBCX_HPRT(usb->index),
3487 cvmx_usbcx_hprt, prtpwr, 1);
3488 spin_unlock_irqrestore(&usb->lock, flags);
3489 return 0;
3490 case USB_PORT_FEAT_RESET:
3491 dev_dbg(dev, " RESET\n");
3492 spin_lock_irqsave(&usb->lock, flags);
3493 cvmx_usb_reset_port(usb);
3494 spin_unlock_irqrestore(&usb->lock, flags);
3495 return 0;
3496 case USB_PORT_FEAT_INDICATOR:
3497 dev_dbg(dev, " INDICATOR\n");
3498
3499 break;
3500 default:
3501 dev_dbg(dev, " UNKNOWN\n");
3502 return -EINVAL;
3503 }
3504 break;
3505 default:
3506 dev_dbg(dev, "Unknown root hub request\n");
3507 return -EINVAL;
3508 }
3509 return 0;
3510 }
3511
3512 static const struct hc_driver octeon_hc_driver = {
3513 .description = "Octeon USB",
3514 .product_desc = "Octeon Host Controller",
3515 .hcd_priv_size = sizeof(struct octeon_hcd),
3516 .irq = octeon_usb_irq,
3517 .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
3518 .start = octeon_usb_start,
3519 .stop = octeon_usb_stop,
3520 .urb_enqueue = octeon_usb_urb_enqueue,
3521 .urb_dequeue = octeon_usb_urb_dequeue,
3522 .endpoint_disable = octeon_usb_endpoint_disable,
3523 .get_frame_number = octeon_usb_get_frame_number,
3524 .hub_status_data = octeon_usb_hub_status_data,
3525 .hub_control = octeon_usb_hub_control,
3526 .map_urb_for_dma = octeon_map_urb_for_dma,
3527 .unmap_urb_for_dma = octeon_unmap_urb_for_dma,
3528 };
3529
3530 static int octeon_usb_probe(struct platform_device *pdev)
3531 {
3532 int status;
3533 int initialize_flags;
3534 int usb_num;
3535 struct resource *res_mem;
3536 struct device_node *usbn_node;
3537 int irq = platform_get_irq(pdev, 0);
3538 struct device *dev = &pdev->dev;
3539 struct octeon_hcd *usb;
3540 struct usb_hcd *hcd;
3541 u32 clock_rate = 48000000;
3542 bool is_crystal_clock = false;
3543 const char *clock_type;
3544 int i;
3545
3546 if (!dev->of_node) {
3547 dev_err(dev, "Error: empty of_node\n");
3548 return -ENXIO;
3549 }
3550 usbn_node = dev->of_node->parent;
3551
3552 i = of_property_read_u32(usbn_node,
3553 "clock-frequency", &clock_rate);
3554 if (i)
3555 i = of_property_read_u32(usbn_node,
3556 "refclk-frequency", &clock_rate);
3557 if (i) {
3558 dev_err(dev, "No USBN \"clock-frequency\"\n");
3559 return -ENXIO;
3560 }
3561 switch (clock_rate) {
3562 case 12000000:
3563 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ;
3564 break;
3565 case 24000000:
3566 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ;
3567 break;
3568 case 48000000:
3569 initialize_flags = CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ;
3570 break;
3571 default:
3572 dev_err(dev, "Illegal USBN \"clock-frequency\" %u\n",
3573 clock_rate);
3574 return -ENXIO;
3575 }
3576
3577 i = of_property_read_string(usbn_node,
3578 "cavium,refclk-type", &clock_type);
3579 if (i)
3580 i = of_property_read_string(usbn_node,
3581 "refclk-type", &clock_type);
3582
3583 if (!i && strcmp("crystal", clock_type) == 0)
3584 is_crystal_clock = true;
3585
3586 if (is_crystal_clock)
3587 initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI;
3588 else
3589 initialize_flags |= CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND;
3590
3591 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3592 if (!res_mem) {
3593 dev_err(dev, "found no memory resource\n");
3594 return -ENXIO;
3595 }
3596 usb_num = (res_mem->start >> 44) & 1;
3597
3598 if (irq < 0) {
3599
3600 irq_hw_number_t hwirq = usb_num ? (1 << 6) + 17 : 56;
3601
3602 irq = irq_create_mapping(NULL, hwirq);
3603 }
3604
3605
3606
3607
3608
3609 i = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
3610 if (i)
3611 return i;
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624 if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
3625 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
3626
3627 pri_cnt.u64 = 0;
3628 pri_cnt.s.cnt_enb = 1;
3629 pri_cnt.s.cnt_val = 400;
3630 cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
3631 }
3632
3633 hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
3634 if (!hcd) {
3635 dev_dbg(dev, "Failed to allocate memory for HCD\n");
3636 return -1;
3637 }
3638 hcd->uses_new_polling = 1;
3639 usb = (struct octeon_hcd *)hcd->hcd_priv;
3640
3641 spin_lock_init(&usb->lock);
3642
3643 usb->init_flags = initialize_flags;
3644
3645
3646 usb->index = usb_num;
3647 INIT_LIST_HEAD(&usb->idle_pipes);
3648 for (i = 0; i < ARRAY_SIZE(usb->active_pipes); i++)
3649 INIT_LIST_HEAD(&usb->active_pipes[i]);
3650
3651
3652 if (OCTEON_IS_MODEL(OCTEON_CN31XX)) {
3653 usb->init_flags |= CVMX_USB_INITIALIZE_FLAGS_NO_DMA;
3654
3655 usb->idle_hardware_channels = 0x1;
3656 } else if (OCTEON_IS_MODEL(OCTEON_CN5XXX)) {
3657
3658 usb->idle_hardware_channels = 0xf7;
3659 } else {
3660 usb->idle_hardware_channels = 0xff;
3661 }
3662
3663 status = cvmx_usb_initialize(dev, usb);
3664 if (status) {
3665 dev_dbg(dev, "USB initialization failed with %d\n", status);
3666 usb_put_hcd(hcd);
3667 return -1;
3668 }
3669
3670 status = usb_add_hcd(hcd, irq, 0);
3671 if (status) {
3672 dev_dbg(dev, "USB add HCD failed with %d\n", status);
3673 usb_put_hcd(hcd);
3674 return -1;
3675 }
3676 device_wakeup_enable(hcd->self.controller);
3677
3678 dev_info(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
3679
3680 return 0;
3681 }
3682
3683 static int octeon_usb_remove(struct platform_device *pdev)
3684 {
3685 int status;
3686 struct device *dev = &pdev->dev;
3687 struct usb_hcd *hcd = dev_get_drvdata(dev);
3688 struct octeon_hcd *usb = hcd_to_octeon(hcd);
3689 unsigned long flags;
3690
3691 usb_remove_hcd(hcd);
3692 spin_lock_irqsave(&usb->lock, flags);
3693 status = cvmx_usb_shutdown(usb);
3694 spin_unlock_irqrestore(&usb->lock, flags);
3695 if (status)
3696 dev_dbg(dev, "USB shutdown failed with %d\n", status);
3697
3698 usb_put_hcd(hcd);
3699
3700 return 0;
3701 }
3702
3703 static const struct of_device_id octeon_usb_match[] = {
3704 {
3705 .compatible = "cavium,octeon-5750-usbc",
3706 },
3707 {},
3708 };
3709 MODULE_DEVICE_TABLE(of, octeon_usb_match);
3710
3711 static struct platform_driver octeon_usb_driver = {
3712 .driver = {
3713 .name = "octeon-hcd",
3714 .of_match_table = octeon_usb_match,
3715 },
3716 .probe = octeon_usb_probe,
3717 .remove = octeon_usb_remove,
3718 };
3719
3720 static int __init octeon_usb_driver_init(void)
3721 {
3722 if (usb_disabled())
3723 return 0;
3724
3725 return platform_driver_register(&octeon_usb_driver);
3726 }
3727 module_init(octeon_usb_driver_init);
3728
3729 static void __exit octeon_usb_driver_exit(void)
3730 {
3731 if (usb_disabled())
3732 return;
3733
3734 platform_driver_unregister(&octeon_usb_driver);
3735 }
3736 module_exit(octeon_usb_driver_exit);
3737
3738 MODULE_LICENSE("GPL");
3739 MODULE_AUTHOR("Cavium, Inc. <support@cavium.com>");
3740 MODULE_DESCRIPTION("Cavium Inc. OCTEON USB Host driver.");