usb.sv 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // usb.sv
  3. //
  4. // For now, just instantiate a USB <-> serial bridge and attach it
  5. // to the output of the tty
  6. //
  7. module max80_usb
  8. #( parameter [2:0] channels = 3'd4 )
  9. (
  10. input hard_rst_n,
  11. input clock48,
  12. input rst_n,
  13. input sys_clk,
  14. input cpu_valid_usbdesc,
  15. input cpu_valid_cdc,
  16. input [31:0] cpu_addr,
  17. output [31:0] cpu_rdata_usbdesc,
  18. output [31:0] cpu_rdata_cdc,
  19. input [31:0] cpu_wdata,
  20. input [3:0] cpu_wstrb,
  21. output [channels-1:0] irq,
  22. output tty_rxd_break, // Break on channel 0
  23. inout usb_dp, // Single ended D+
  24. inout usb_dn, // Single ended D-
  25. input usb_rx, // Differential input
  26. input usb_rx_ok, // Differential input available
  27. output usb_pu // Driver for 1.5 kohm pullup
  28. );
  29. //
  30. // BREAK status by channel
  31. //
  32. wire [channels-1:0] recv_break;
  33. assign tty_rxd_break = recv_break[0]; // = system soft reset
  34. //
  35. // UTMI interface to PHY
  36. //
  37. wire [7:0] utmi_data_out;
  38. wire [1:0] utmi_op_mode;
  39. wire [1:0] utmi_xcvrselect;
  40. wire utmi_termselect;
  41. wire utmi_dppulldown;
  42. wire utmi_dmpulldown;
  43. wire [7:0] utmi_data_in;
  44. wire utmi_txvalid;
  45. wire utmi_txready;
  46. wire utmi_rxvalid;
  47. wire utmi_rxactive;
  48. wire utmi_rxerror;
  49. wire [1:0] utmi_linestate;
  50. //
  51. // USB hardware interface to PHY
  52. //
  53. wire usb_rx_rcv = usb_rx_ok ? usb_rx : usb_dp & ~usb_dn;
  54. wire usb_rx_dp = usb_dp;
  55. wire usb_rx_dn = usb_dn;
  56. wire usb_tx_dp;
  57. wire usb_tx_dn;
  58. wire usb_tx_oen;
  59. wire usb_en;
  60. //
  61. // Reset: reset USB on hard_rst# but synchronize to the USB clock
  62. //
  63. reg usb_rst_n;
  64. always @(negedge hard_rst_n or posedge clock48)
  65. if (~hard_rst_n)
  66. usb_rst_n <= 1'b0;
  67. else
  68. usb_rst_n <= 1'b1;
  69. //
  70. // I/O pins to PHY
  71. //
  72. assign usb_dp = ( usb_rst_n & ~usb_tx_oen ) ? usb_tx_dp : 1'bz;
  73. assign usb_dn = ( usb_rst_n & ~usb_tx_oen ) ? usb_tx_dn : 1'bz;
  74. assign usb_pu = ( usb_rst_n & usb_en ) ? 1'b1 : 1'bz;
  75. usb_fs_phy usb_phy (
  76. .clk_i ( clock48 ),
  77. .rst_i ( ~usb_rst_n ),
  78. .utmi_data_out_i ( utmi_data_out ),
  79. .utmi_txvalid_i ( utmi_txvalid ),
  80. .utmi_op_mode_i ( utmi_op_mode ),
  81. .utmi_xcvrselect_i ( utmi_xcvrselect ),
  82. .utmi_termselect_i ( utmi_termselect ),
  83. .utmi_dppulldown_i ( utmi_dppulldown ),
  84. .utmi_dmpulldown_i ( utmi_dmpulldown ),
  85. .usb_rx_rcv_i ( usb_rx_rcv ),
  86. .usb_rx_dp_i ( usb_rx_dp ),
  87. .usb_rx_dn_i ( usb_rx_dn ),
  88. .usb_reset_assert_i ( 1'b0 ),
  89. .utmi_data_in_o ( utmi_data_in ),
  90. .utmi_txready_o ( utmi_txready ),
  91. .utmi_rxvalid_o ( utmi_rxvalid ),
  92. .utmi_rxactive_o ( utmi_rxactive ),
  93. .utmi_rxerror_o ( utmi_rxerror ),
  94. .utmi_linestate_o ( utmi_linestate ),
  95. .usb_tx_dp_o ( usb_tx_dp ),
  96. .usb_tx_dn_o ( usb_tx_dn ),
  97. .usb_tx_oen_o ( usb_tx_oen ),
  98. .usb_reset_detect_o ( ),
  99. .usb_en_o ( usb_en )
  100. );
  101. usb_cdc_core #(.channels(channels))
  102. usb_serial (
  103. .clk_i ( clock48 ),
  104. .rst_i ( ~usb_rst_n ),
  105. .enable_i ( 1'b1 ),
  106. .utmi_data_in_i ( utmi_data_in ),
  107. .utmi_txready_i ( utmi_txready ),
  108. .utmi_rxvalid_i ( utmi_rxvalid ),
  109. .utmi_rxactive_i ( utmi_rxactive ),
  110. .utmi_rxerror_i ( utmi_rxerror ),
  111. .utmi_linestate_i ( utmi_linestate ),
  112. .utmi_data_out_o ( utmi_data_out ),
  113. .utmi_txvalid_o ( utmi_txvalid ),
  114. .utmi_op_mode_o ( utmi_op_mode ),
  115. .utmi_xcvrselect_o ( utmi_xcvrselect ),
  116. .utmi_termselect_o ( utmi_termselect ),
  117. .utmi_dppulldown_o ( utmi_dppulldown ),
  118. .utmi_dmpulldown_o ( utmi_dmpulldown ),
  119. .rst_n ( rst_n ),
  120. .sys_clk ( sys_clk ),
  121. .cpu_valid_usbdesc ( cpu_valid_usbdesc ),
  122. .cpu_valid_cdc ( cpu_valid_cdc ),
  123. .cpu_addr ( cpu_addr ),
  124. .cpu_rdata_usbdesc ( cpu_rdata_usbdesc ),
  125. .cpu_rdata_cdc ( cpu_rdata_cdc ),
  126. .cpu_wdata ( cpu_wdata ),
  127. .cpu_wstrb ( cpu_wstrb ),
  128. .irq ( irq ),
  129. .recv_break_o ( recv_break )
  130. );
  131. endmodule // max80_usb