usb_phy.v 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * usb_phy.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut
  7. * All rights reserved.
  8. *
  9. * LGPL v3+, see LICENSE.lgpl3
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. `default_nettype none
  26. module usb_phy #(
  27. parameter TARGET = "ICE40"
  28. )(
  29. // Pads
  30. inout wire pad_dp,
  31. inout wire pad_dn,
  32. // RX
  33. output wire rx_dp,
  34. output wire rx_dn,
  35. output wire rx_chg,
  36. // TX
  37. input wire tx_dp,
  38. input wire tx_dn,
  39. input wire tx_en,
  40. // Common
  41. input wire clk,
  42. input wire rst
  43. );
  44. wire [1:0] rx_dp_i;
  45. wire [1:0] rx_dn_i;
  46. reg [2:0] dp_state;
  47. reg [2:0] dn_state;
  48. // IO buffers
  49. generate
  50. if (TARGET == "ICE40") begin
  51. SB_IO #(
  52. .PIN_TYPE(6'b110100),
  53. .PULLUP(1'b0),
  54. .NEG_TRIGGER(1'b0),
  55. .IO_STANDARD("SB_LVCMOS")
  56. ) io_dp_I (
  57. .PACKAGE_PIN(pad_dp),
  58. .LATCH_INPUT_VALUE(1'b0),
  59. .CLOCK_ENABLE(1'b1),
  60. .INPUT_CLK(clk),
  61. .OUTPUT_CLK(clk),
  62. .OUTPUT_ENABLE(tx_en),
  63. .D_OUT_0(tx_dp),
  64. .D_OUT_1(1'b0),
  65. .D_IN_0(rx_dp_i[0]),
  66. .D_IN_1(rx_dp_i[1])
  67. );
  68. SB_IO #(
  69. .PIN_TYPE(6'b110100),
  70. .PULLUP(1'b0),
  71. .NEG_TRIGGER(1'b0),
  72. .IO_STANDARD("SB_LVCMOS")
  73. ) io_dn_I (
  74. .PACKAGE_PIN(pad_dn),
  75. .LATCH_INPUT_VALUE(1'b0),
  76. .CLOCK_ENABLE(1'b1),
  77. .INPUT_CLK(clk),
  78. .OUTPUT_CLK(clk),
  79. .OUTPUT_ENABLE(tx_en),
  80. .D_OUT_0(tx_dn),
  81. .D_OUT_1(1'b0),
  82. .D_IN_0(rx_dn_i[0]),
  83. .D_IN_1(rx_dn_i[1])
  84. );
  85. end
  86. endgenerate
  87. // Input sync, filter and change detect
  88. always @(posedge clk or posedge rst)
  89. begin
  90. if (rst) begin
  91. dp_state <= 3'b000;
  92. dn_state <= 3'b000;
  93. end else begin
  94. case ({dp_state[1:0], rx_dp_i})
  95. 4'b0000: dp_state <= 3'b000;
  96. 4'b0001: dp_state <= 3'b001;
  97. 4'b0010: dp_state <= 3'b001;
  98. 4'b0011: dp_state <= 3'b001;
  99. 4'b0100: dp_state <= 3'b000;
  100. 4'b0101: dp_state <= 3'b001;
  101. 4'b0110: dp_state <= 3'b001;
  102. 4'b0111: dp_state <= 3'b111;
  103. 4'b1000: dp_state <= 3'b100;
  104. 4'b1001: dp_state <= 3'b010;
  105. 4'b1010: dp_state <= 3'b010;
  106. 4'b1011: dp_state <= 3'b011;
  107. 4'b1100: dp_state <= 3'b010;
  108. 4'b1101: dp_state <= 3'b010;
  109. 4'b1110: dp_state <= 3'b010;
  110. 4'b1111: dp_state <= 3'b011;
  111. default: dp_state <= 3'bxxx;
  112. endcase
  113. case ({dn_state[1:0], rx_dn_i})
  114. 4'b0000: dn_state <= 3'b000;
  115. 4'b0001: dn_state <= 3'b001;
  116. 4'b0010: dn_state <= 3'b001;
  117. 4'b0011: dn_state <= 3'b001;
  118. 4'b0100: dn_state <= 3'b000;
  119. 4'b0101: dn_state <= 3'b001;
  120. 4'b0110: dn_state <= 3'b001;
  121. 4'b0111: dn_state <= 3'b111;
  122. 4'b1000: dn_state <= 3'b100;
  123. 4'b1001: dn_state <= 3'b010;
  124. 4'b1010: dn_state <= 3'b010;
  125. 4'b1011: dn_state <= 3'b011;
  126. 4'b1100: dn_state <= 3'b010;
  127. 4'b1101: dn_state <= 3'b010;
  128. 4'b1110: dn_state <= 3'b010;
  129. 4'b1111: dn_state <= 3'b011;
  130. default: dn_state <= 3'bxxx;
  131. endcase
  132. end
  133. end
  134. assign rx_dp = dp_state[1];
  135. assign rx_dn = dn_state[1];
  136. assign rx_chg = dp_state[2] | dn_state[2];
  137. endmodule // usb_phy